简体   繁体   中英

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.

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";
}

The best way is to configure the $classnameLabel variable in the Admin Class :

class fooAdmin extends Admin
{
    protected $classnameLabel = 'Custom Label';
}

But it have the same issue (weird name with entity path) doing it, even if it is working fine on all the others pages.

Apparently, the Sonata way of solving this is show here:

Quote:

While it's very friendly of the SonataAdminBundle to notify the admin of a successful creation, the classname and some sort of hash aren't really nice to read. This is the default string representation of an object in the SonataAdminBundle. You can change it by defining a toString() (note: no underscore prefix) method in the Admin class. This receives the object to transform to a string as the first parameter:

Source: https://sonata-project.org/bundles/admin/master/doc/getting_started/the_form_view.html#creating-a-blog-post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM