From 6e121e16bc4e4aaab83288bb1c868d43f24ee61a Mon Sep 17 00:00:00 2001 From: Kristof Date: Fri, 8 Aug 2025 15:40:50 +0200 Subject: [PATCH 1/4] Added resource and global search on page resource --- src/Resources/PageResource.php | 48 ++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/Resources/PageResource.php b/src/Resources/PageResource.php index 03a9d2e..910a767 100644 --- a/src/Resources/PageResource.php +++ b/src/Resources/PageResource.php @@ -12,6 +12,8 @@ use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Str; use Statikbe\FilamentFlexibleContentBlockPages\Actions\LinkedToMenuItemBulkDeleteAction; use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages; use Statikbe\FilamentFlexibleContentBlockPages\Resources\PageResource\Pages\CreatePage; @@ -47,6 +49,10 @@ class PageResource extends Resource protected static ?string $recordTitleAttribute = 'title'; + protected static int $globalSearchResultsLimit = 10; + + protected static ?bool $isGlobalSearchForcedCaseInsensitive = true; + public static function getModel(): string { return FilamentFlexibleContentBlockPages::config()->getPageModel()::class; @@ -179,12 +185,18 @@ public static function table(Table $table): Table { return $table ->columns([ - TitleColumn::create(), + TitleColumn::create() + ->searchable(query: function($query, $search) { + $locale = app()->getLocale(); + $search = strtolower($search); + return $query->whereRaw("LOWER(title->>'$.{$locale}') LIKE ?", ["%{$search}%"]); + }), TextColumn::make('created_at') ->label(flexiblePagesTrans('pages.table.created_at_col')) ->dateTime(FilamentFlexibleBlocksConfig::getPublishingDateFormatting()) ->sortable(), - PublishedColumn::create(), + PublishedColumn::create() + ->sortable(), ]) ->filters([ PublishedFilter::create(), @@ -220,4 +232,36 @@ public static function getPages(): array 'edit' => EditPage::route('/{record:id}/edit'), ]; } + + public static function getGloballySearchableAttributes(): array + { + return [ + 'title', + 'intro', + 'content_blocks', + 'seo_title', + 'seo_description', + 'seo_keywords' + ]; + } + + public static function getGlobalSearchResultTitle(Model $record): string + { + return $record->getTranslation('title', app()->getLocale()); + } + + public static function getGlobalSearchResultDetails(Model $record): array + { + $published = trans('filament-flexible-content-blocks::filament-flexible-content-blocks.columns.is_published_state_unpublished'); + if ($record->isPublished()) { + $published = trans( + 'filament-flexible-content-blocks::filament-flexible-content-blocks.columns.is_published_state_published' + ); + } + + return [ + trans('filament-flexible-content-blocks::filament-flexible-content-blocks.form_component.intro_lbl') => Str::limit(strip_tags($record->intro)), + trans('filament-flexible-content-blocks::filament-flexible-content-blocks.columns.is_published') => $published + ]; + } } From 9208c017765af6a32301657bd7e41fd15de526cb Mon Sep 17 00:00:00 2001 From: kristofser <5928907+kristofser@users.noreply.github.com> Date: Fri, 8 Aug 2025 13:41:23 +0000 Subject: [PATCH 2/4] Fix styling --- src/Resources/PageResource.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Resources/PageResource.php b/src/Resources/PageResource.php index 910a767..23479c5 100644 --- a/src/Resources/PageResource.php +++ b/src/Resources/PageResource.php @@ -186,11 +186,12 @@ public static function table(Table $table): Table return $table ->columns([ TitleColumn::create() - ->searchable(query: function($query, $search) { - $locale = app()->getLocale(); - $search = strtolower($search); - return $query->whereRaw("LOWER(title->>'$.{$locale}') LIKE ?", ["%{$search}%"]); - }), + ->searchable(query: function ($query, $search) { + $locale = app()->getLocale(); + $search = strtolower($search); + + return $query->whereRaw("LOWER(title->>'$.{$locale}') LIKE ?", ["%{$search}%"]); + }), TextColumn::make('created_at') ->label(flexiblePagesTrans('pages.table.created_at_col')) ->dateTime(FilamentFlexibleBlocksConfig::getPublishingDateFormatting()) @@ -241,7 +242,7 @@ public static function getGloballySearchableAttributes(): array 'content_blocks', 'seo_title', 'seo_description', - 'seo_keywords' + 'seo_keywords', ]; } @@ -261,7 +262,7 @@ public static function getGlobalSearchResultDetails(Model $record): array return [ trans('filament-flexible-content-blocks::filament-flexible-content-blocks.form_component.intro_lbl') => Str::limit(strip_tags($record->intro)), - trans('filament-flexible-content-blocks::filament-flexible-content-blocks.columns.is_published') => $published + trans('filament-flexible-content-blocks::filament-flexible-content-blocks.columns.is_published') => $published, ]; } } From bfe16866aa9765b316cfbfa4673b82a78e16756a Mon Sep 17 00:00:00 2001 From: Kristof Date: Mon, 11 Aug 2025 08:39:02 +0200 Subject: [PATCH 3/4] Added global and table search --- src/Models/Concerns/HasSearchFilterTrait.php | 24 +++++++++++++++++++ src/Models/Page.php | 2 ++ src/Resources/PageResource.php | 4 +++- .../PageResource/Pages/ListPages.php | 10 ++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/Models/Concerns/HasSearchFilterTrait.php diff --git a/src/Models/Concerns/HasSearchFilterTrait.php b/src/Models/Concerns/HasSearchFilterTrait.php new file mode 100644 index 0000000..1b3da9a --- /dev/null +++ b/src/Models/Concerns/HasSearchFilterTrait.php @@ -0,0 +1,24 @@ +when($search, function ($query, $search) { + $locale = app()->getLocale(); + $query->where(function($query) use ($search, $locale) { + $query->whereRaw("LOWER(title->>'$.{$locale}') LIKE ?", ["%{$search}%"]) + ->orWhereRaw("LOWER(intro->>'$.{$locale}') LIKE ?", ["%{$search}%"]) + ->orWhereRaw("LOWER(content_blocks->>'$.{$locale}') LIKE ?", ["%{$search}%"]) + ->orWhereRaw("LOWER(seo_title->>'$.{$locale}') LIKE ?", ["%{$search}%"]) + ->orWhereRaw("LOWER(seo_description->>'$.{$locale}') LIKE ?", ["%{$search}%"]) + ->orWhereRaw("LOWER(overview_title->>'$.{$locale}') LIKE ?", ["%{$search}%"]) + ->orWhereRaw("LOWER(overview_description->>'$.{$locale}') LIKE ?", ["%{$search}%"]); + }); + }); + } +} diff --git a/src/Models/Page.php b/src/Models/Page.php index 7630528..c309feb 100644 --- a/src/Models/Page.php +++ b/src/Models/Page.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\HasMedia; use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages; +use Statikbe\FilamentFlexibleContentBlockPages\Models\Concerns\HasSearchFilterTrait; use Statikbe\FilamentFlexibleContentBlockPages\Models\Concerns\HasTitleMenuLabelTrait; use Statikbe\FilamentFlexibleContentBlockPages\Models\Contracts\HasMenuLabel; use Statikbe\FilamentFlexibleContentBlockPages\Observers\PageObserver; @@ -53,6 +54,7 @@ class Page extends Model implements HasCode, HasContentBlocks, HasHeroCallToActi use HasTranslatedPageAttributesTrait; use HasTranslatedSEOAttributesTrait; use HasTranslatedSlugAttributeTrait; + use HasSearchFilterTrait; const HOME_PAGE = 'HOME'; diff --git a/src/Resources/PageResource.php b/src/Resources/PageResource.php index 910a767..cefe672 100644 --- a/src/Resources/PageResource.php +++ b/src/Resources/PageResource.php @@ -241,7 +241,9 @@ public static function getGloballySearchableAttributes(): array 'content_blocks', 'seo_title', 'seo_description', - 'seo_keywords' + 'seo_keywords', + 'overview_title', + 'overview_description', ]; } diff --git a/src/Resources/PageResource/Pages/ListPages.php b/src/Resources/PageResource/Pages/ListPages.php index 4b874fb..188dfc2 100644 --- a/src/Resources/PageResource/Pages/ListPages.php +++ b/src/Resources/PageResource/Pages/ListPages.php @@ -5,6 +5,7 @@ use Filament\Actions\CreateAction; use Filament\Resources\Pages\ListRecords; use Filament\Resources\Pages\ListRecords\Concerns\Translatable; +use Illuminate\Database\Eloquent\Builder; use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages; use Statikbe\FilamentFlexibleContentBlockPages\FilamentFlexibleContentBlockPagesConfig; use Statikbe\FilamentFlexibleContentBlocks\Filament\Actions\FlexibleLocaleSwitcher; @@ -30,4 +31,13 @@ public function isTableSearchable(): bool { return true; } + + protected function applySearchToTableQuery(Builder $query): Builder + { + if (filled($searchQuery = $this->getTableSearch())) { + $query->search($searchQuery); + } + + return $query; + } } From 75c3261ca044ab09da2a88c4d9657189aef575a7 Mon Sep 17 00:00:00 2001 From: kristofser <5928907+kristofser@users.noreply.github.com> Date: Mon, 11 Aug 2025 06:40:39 +0000 Subject: [PATCH 4/4] Fix styling --- src/Models/Concerns/HasSearchFilterTrait.php | 3 +-- src/Models/Page.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Models/Concerns/HasSearchFilterTrait.php b/src/Models/Concerns/HasSearchFilterTrait.php index 1b3da9a..603055d 100644 --- a/src/Models/Concerns/HasSearchFilterTrait.php +++ b/src/Models/Concerns/HasSearchFilterTrait.php @@ -4,13 +4,12 @@ trait HasSearchFilterTrait { - public function scopeSearch($query, string $search): void { $search = strtolower($search); $query->when($search, function ($query, $search) { $locale = app()->getLocale(); - $query->where(function($query) use ($search, $locale) { + $query->where(function ($query) use ($search, $locale) { $query->whereRaw("LOWER(title->>'$.{$locale}') LIKE ?", ["%{$search}%"]) ->orWhereRaw("LOWER(intro->>'$.{$locale}') LIKE ?", ["%{$search}%"]) ->orWhereRaw("LOWER(content_blocks->>'$.{$locale}') LIKE ?", ["%{$search}%"]) diff --git a/src/Models/Page.php b/src/Models/Page.php index c309feb..57617fb 100644 --- a/src/Models/Page.php +++ b/src/Models/Page.php @@ -46,6 +46,7 @@ class Page extends Model implements HasCode, HasContentBlocks, HasHeroCallToActi use HasFactory; use HasHeroCallToActionsTrait; use HasParentTrait; + use HasSearchFilterTrait; use HasTitleMenuLabelTrait; use HasTranslatedContentBlocksTrait; use HasTranslatedHeroImageAttributesTrait; @@ -54,7 +55,6 @@ class Page extends Model implements HasCode, HasContentBlocks, HasHeroCallToActi use HasTranslatedPageAttributesTrait; use HasTranslatedSEOAttributesTrait; use HasTranslatedSlugAttributeTrait; - use HasSearchFilterTrait; const HOME_PAGE = 'HOME';