Global search
is it possible to add custom pages to the results of global search?
1 Reply
Figured it out,
Add the below to getResults in Filament\GlobalSearch\DefaultGlobalSearchProvider, probably need some additional work to see if the user has access etc.
Add listFilamentPages to class.
$pages = self::listFilamentPages();
foreach ($pages as $pageClass => $pageTitle) {
if (Str::contains(strtolower($pageTitle), strtolower($query))) {
$pageResult = new GlobalSearchResult(
$pageTitle,
$pageClass::getUrl()
);
$builder->category('Pages', collect([$pageResult]));
}
}
$pages = self::listFilamentPages();
foreach ($pages as $pageClass => $pageTitle) {
if (Str::contains(strtolower($pageTitle), strtolower($query))) {
$pageResult = new GlobalSearchResult(
$pageTitle,
$pageClass::getUrl()
);
$builder->category('Pages', collect([$pageResult]));
}
}
public function listFilamentPages(): array
{
$pagesDirectory = app_path('Filament/Pages');
$namespace = 'App\\Filament\\Pages\\';
$pageClasses = [];
foreach (glob($pagesDirectory . '/*.php') as $filePath) {
$class = $namespace . pathinfo($filePath, PATHINFO_FILENAME);
try {
$reflection = new ReflectionClass($class);
if ($reflection->isSubclassOf(Page::class) && !$reflection->isAbstract()) {
if (method_exists($class, 'getTitle')) {
$instance = app($class);
$pageTitle = $instance->getTitle();
} else {
$pageTitle = $reflection->getShortName();
}
$pageClasses[$class] = $pageTitle;
}
} catch (ReflectionException $e) {
continue;
}
}
return $pageClasses;
}
public function listFilamentPages(): array
{
$pagesDirectory = app_path('Filament/Pages');
$namespace = 'App\\Filament\\Pages\\';
$pageClasses = [];
foreach (glob($pagesDirectory . '/*.php') as $filePath) {
$class = $namespace . pathinfo($filePath, PATHINFO_FILENAME);
try {
$reflection = new ReflectionClass($class);
if ($reflection->isSubclassOf(Page::class) && !$reflection->isAbstract()) {
if (method_exists($class, 'getTitle')) {
$instance = app($class);
$pageTitle = $instance->getTitle();
} else {
$pageTitle = $reflection->getShortName();
}
$pageClasses[$class] = $pageTitle;
}
} catch (ReflectionException $e) {
continue;
}
}
return $pageClasses;
}