Skip to content

Commit a920f63

Browse files
committed
Default menu label impl trait
1 parent 3ddaff5 commit a920f63

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Statikbe\FilamentFlexibleContentBlockPages\Models\Concerns;
4+
5+
trait HasTitleMenuLabelTrait
6+
{
7+
/**
8+
* Get the display label for menu items.
9+
* This implementation uses the 'title' field with locale support.
10+
*/
11+
public function getMenuLabel(?string $locale = null): string
12+
{
13+
$locale = $locale ?: app()->getLocale();
14+
15+
// Check if the model uses translations (like Spatie Laravel Translatable)
16+
if (method_exists($this, 'getTranslation')) {
17+
$title = $this->getTranslation('title', $locale);
18+
if (empty($title)) {
19+
// Fallback to the configured fallback locale
20+
$title = $this->getTranslation('title', config('app.fallback_locale', 'en'));
21+
}
22+
return $title ?: 'Untitled';
23+
}
24+
25+
return $this->title ?: 'Untitled';
26+
}
27+
28+
/**
29+
* Scope to search for models that can be used in menu items.
30+
* This implementation searches in common searchable fields.
31+
*/
32+
public function scopeSearchForMenuItems($query, string $search)
33+
{
34+
return $query->where(function ($query) use ($search) {
35+
$query->where('title', 'like', "%{$search}%");
36+
37+
// Add additional searchable fields if they exist
38+
$searchableFields = ['name', 'intro', 'description', 'overview_title'];
39+
foreach ($searchableFields as $field) {
40+
if (in_array($field, $this->getFillable()) ||
41+
(method_exists($this, 'getTranslatableAttributes') &&
42+
in_array($field, $this->getTranslatableAttributes()))) {
43+
$query->orWhere($field, 'like', "%{$search}%");
44+
}
45+
}
46+
});
47+
}
48+
}

src/Models/Page.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Spatie\MediaLibrary\HasMedia;
88
use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages;
99
use Statikbe\FilamentFlexibleContentBlockPages\Models\Contracts\HasMenuLabel;
10+
use Statikbe\FilamentFlexibleContentBlockPages\Models\Concerns\HasTitleMenuLabelTrait;
1011
use Statikbe\FilamentFlexibleContentBlocks\Models\Concerns\HasAuthorAttributeTrait;
1112
use Statikbe\FilamentFlexibleContentBlocks\Models\Concerns\HasCodeTrait;
1213
use Statikbe\FilamentFlexibleContentBlocks\Models\Concerns\HasDefaultContentBlocksTrait;
@@ -38,6 +39,7 @@ class Page extends Model implements HasCode, HasContentBlocks, HasHeroImageAttri
3839
use HasDefaultContentBlocksTrait;
3940
use HasFactory;
4041
use HasParentTrait;
42+
use HasTitleMenuLabelTrait;
4143
use HasTranslatedContentBlocksTrait;
4244
use HasTranslatedHeroImageAttributesTrait;
4345
use HasTranslatedIntroAttributeTrait;
@@ -88,19 +90,4 @@ public function getMorphClass()
8890
return 'filament-flexible-content-block-pages::page';
8991
}
9092

91-
public function getMenuLabel(?string $locale = null): string
92-
{
93-
$locale = $locale ?: app()->getLocale();
94-
95-
return $this->getTranslation('title', $locale) ?: $this->getTranslation('title', config('app.fallback_locale', 'en'));
96-
}
97-
98-
public function scopeSearchForMenuItems($query, string $search)
99-
{
100-
return $query->where(function ($query) use ($search) {
101-
$query->where('title', 'like', "%{$search}%")
102-
->orWhere('intro', 'like', "%{$search}%")
103-
->orWhere('overview_title', 'like', "%{$search}%");
104-
});
105-
}
10693
}

0 commit comments

Comments
 (0)