Giter Club home page Giter Club logo

Comments (1)

zyberspace avatar zyberspace commented on June 20, 2024

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!

from sonataadminbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.