Closed ozahorulia closed 7 months ago
Hi @ozahorulia ,
in case you don't know, you don't always need to extend the original class. Symfony provides the so called "Decorator pattern": https://symfony.com/doc/current/service_container/service_decoration.html
With this, you can basically wrap the original breadcrumbs builder ("decorate") and either alter its input or change its output. Only thing you need to do is implement the correct interface, so sonata (and you) knows that all the required methods are available, and mark your new service with #[AsDecorator(...)]
.
E.g.:
<?php
declare(strict_types=1);
namespace App;
use Knp\Menu\MenuItem;
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
#[AsDecorator(decorates: '@sonata.admin.breadcrumbs_builder')]
class MyCustomBreadcrumbsBuilder implements BreadcrumbsBuilderInterface
{
public function __construct(
#[AutowireDecorated]
private BreadcrumbsBuilderInterface $decorated,
) {
}
public function getBreadcrumbs(AdminInterface $admin, string $action): array
{
// Call the original breadcrumbs builder first
$breadcrumbs = $this->decorated->getBreadcrumbs($admin, $action);
// Detect if breadcrumbs should be changed
// if (...) {
// Change label of last breadcrumb
$lastBreadcrumb = $breadcrumbs[array_key_last(breadcrumbs)];
assert($lastBreadcrumb instanceof MenuItem);
$lastBreadcrumb->setLabel('My custom label');
// }
// Return the updated breadcrumbs list
return $breadcrumbs;
}
}
If you use an older symfony version where the AsDecorator
attribute is not available yet, just change the documentation version to your desired symfony version and you will get instructions for your version instead.
NOTE: I did not test this. Usually i just need breadcrumbs for my custom pages, which i provide manually via the twig breadcrumb
block in the template (setting this block overwrites the default breadcrumb). But this should work just like any other service decoration.
I hope this helps.
Greetings!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Feature Request
Sometimes there are cases which require path in the breadcrumbs to be different from the actual sonata structure. Looks like that right now it's absolutely impossible to do that without completely overloading the
BreadcrumbsBuilder
. And the problem with that is thatBreadcrumbsBuilder
is declared as final, so I'd have to duplicate the class just to make a change to a single admin page. Re-declaring header template in twig is also an overhead (and I'm not even sure it's possible).Should we have this thing to be customizable? Or is there already a way to do that?