F
Filament11mo ago
Brian.

Prepend a page (or other type) to breadcrumbs

Currently I am trying to prepend my custom page "Settings" to some resource pages. I found about the getBreadcrumbs function and tried to overwrite it by simple copying the current one and adding my item like so:
public function getBreadcrumbs(): array {
$resource = static::getResource();

$breadcrumbs = [
'/settings' => "Instellingen",
$resource::getUrl() => $resource::getBreadcrumb(),
...(filled($breadcrumb = $this->getBreadcrumb()) ? [$breadcrumb] : []),
];

if (filled($cluster = static::getCluster())) {
return $cluster::unshiftClusterBreadcrumbs($breadcrumbs);
}

return $breadcrumbs;
}
public function getBreadcrumbs(): array {
$resource = static::getResource();

$breadcrumbs = [
'/settings' => "Instellingen",
$resource::getUrl() => $resource::getBreadcrumb(),
...(filled($breadcrumb = $this->getBreadcrumb()) ? [$breadcrumb] : []),
];

if (filled($cluster = static::getCluster())) {
return $cluster::unshiftClusterBreadcrumbs($breadcrumbs);
}

return $breadcrumbs;
}
The thing is, I have a lot of resources that fall under this "Settings" page. Is there any simpler and/or more correct way, besides making a helper function, to do this? Thanks in advance!
3 Replies
Patabugen
Patabugen4mo ago
I found your post looking for the same, your tip solved my issue! As for having the same/similar settings, I've wondered about using a Trait or as you suggest a helper method. For now, I only have one Resource that needs it - but I wanted to say thanks for your snippet!
Brian.
Brian.OP4mo ago
Glad I could help you with it! Eventually I did just put it in a helper function:
if (! function_exists('getSettingsBreadcrumbs')) {
function getSettingsBreadcrumbs($resource, $current_breadcrumb, $cluster)
{
$breadcrumbs = [
'/admin/settings' => "Instellingen",
$resource::getUrl() => $resource::getBreadcrumb(),
...(filled($breadcrumb = $current_breadcrumb) ? [$breadcrumb] : []),
];

if (filled($cluster)) {
return $cluster::unshiftClusterBreadcrumbs($breadcrumbs);
}

return $breadcrumbs;
}
}
if (! function_exists('getSettingsBreadcrumbs')) {
function getSettingsBreadcrumbs($resource, $current_breadcrumb, $cluster)
{
$breadcrumbs = [
'/admin/settings' => "Instellingen",
$resource::getUrl() => $resource::getBreadcrumb(),
...(filled($breadcrumb = $current_breadcrumb) ? [$breadcrumb] : []),
];

if (filled($cluster)) {
return $cluster::unshiftClusterBreadcrumbs($breadcrumbs);
}

return $breadcrumbs;
}
}
And then used it in Create & Edit like so:
class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;

public function getBreadcrumbs(): array {
return getSettingsBreadcrumbs(
resource: static::getResource(),
current_breadcrumb: self::getBreadcrumb(),
cluster: static::getCluster()
);
}
}
class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;

public function getBreadcrumbs(): array {
return getSettingsBreadcrumbs(
resource: static::getResource(),
current_breadcrumb: self::getBreadcrumb(),
cluster: static::getCluster()
);
}
}
Patabugen
Patabugen4mo ago
ooh handy, thanks for sharing!
Want results from more Discord servers?
Add your server