Search code examples
symfonysonata-adminsymfony-sonata

Sonata Admin - how to set the menu.label attribute?


According to the Sonata source code, the last node in the breadcrumb is rendered this way:

# standard_layout.html.twig #
<li class="active"><span>{{ menu.label }}</span></li>

In my setup, when opening a given Admin subclass, the last node simply becomes a raw string according to the entity handled by the Admin:

Dashboard  /  Entity List  /  Acme\SomeBundle\Entity\Stuff:000000001d74ac0a00007ff2930a326f

How can I set the value of menu.label to get something more appropriate? I have tried, in my Admin subclass, to override the following:

protected function configureTabMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null) {
    $this->configureSideMenu($menu, $action, $childAdmin);


}

protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null) {
    $menu->setLabel("Some nice label");
    $menu->setName("Some nice name");
}

However, this does not change anything, even though I have verified that the methods above are called during runtime.


Solution

  • Finally found a good (and somewhat obvious) solution to this.

    The Sonata Admin class uses an internal toString($object) method in order to get a label string for the entity it is handling. Thus, the key is to implement the __toString() method of the entity in question:

    public function __toString() {
        return "test";
    }