-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHasTitleMenuLabelTrait.php
More file actions
48 lines (41 loc) · 1.64 KB
/
Copy pathHasTitleMenuLabelTrait.php
File metadata and controls
48 lines (41 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace Statikbe\FilamentFlexibleContentBlockPages\Models\Concerns;
trait HasTitleMenuLabelTrait
{
use HasMenuItemTrait;
/**
* Get the display label for menu items.
* This implementation uses the 'title' field with locale support.
* Assumes the model uses Spatie Laravel Translatable trait.
*/
public function getMenuLabel(?string $locale = null): string
{
$locale = $locale ?: app()->getLocale();
// Get translated title (assumes HasTranslations trait is used)
$title = $this->getTranslation('title', $locale);
if (empty($title)) {
// Fallback to the configured fallback locale
$title = $this->getTranslation('title', config('app.fallback_locale', 'en'));
}
return $title ?: 'Untitled';
}
/**
* Scope to search for models that can be used in menu items.
* This implementation searches in common searchable fields.
* Assumes the model uses Spatie Laravel Translatable trait.
*/
public function scopeSearchForMenuItems($query, string $search)
{
return $query->where(function ($query) use ($search) {
$query->where('title', 'like', "%{$search}%");
// Add additional searchable fields if they exist
$searchableFields = ['name', 'intro', 'description', 'overview_title'];
foreach ($searchableFields as $field) {
if (in_array($field, $this->getFillable()) ||
in_array($field, $this->getTranslatableAttributes())) {
$query->orWhere($field, 'like', "%{$search}%");
}
}
});
}
}