sonata admin label on breadcrumb

4.6k views Asked by At

I have a little problem with sonata admin on Symfony.
I would like to change the default admin label in the breadcrumb:

enter image description here

but I can't find any solution. Can someone help me?

I found this function, but it doesn't work. It looks like that this function is not called.

public function buildBreadcrumbs($action, MenuItemInterface $menu = null) {
    $breadCrumb =  parent::buildBreadcrumbs($action, $menu);

    return $breadCrumb;
}

I use Symfony 2.8.

2

There are 2 answers

3
Yehor On BEST ANSWER

Try to override classNameLabel property in your admin class:

// in your ProductAdmin class
public function configure()
{
    $this->classnameLabel = "Products";
}
0
Mawcel On

The simplest way to achieve what you want is to change translations messages.

If you really want to change the labels you can implement your own label generation strategy.

namespace Blast\CoreBundle\Translator;

use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;

/**
 * Class LibrinfoLabelTranslatorStrategy.
 *
 * Provides a specific label translation strategy for Librinfo.
 * It is based on UnderscoreLabelTranslatorStrategy, but without the context,
 * and labels are prefixed by "librinfo.label."
 *
 * i.e. isValid => librinfo.label.is_valid
 */
class LibrinfoLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
{
    /**
     * {@inheritdoc}
     */
    public function getLabel($label, $context = '', $type = '')
    {
        $label = str_replace('.', '_', $label);

        return sprintf('%s.%s.%s', "librinfo", $type, strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label)));
    }
}

define it as a service

blast_core.label.strategy.librinfo:
        class: Blast\CoreBundle\Translator\LibrinfoLabelTranslatorStrategy

then pass it to the definition of your admin service like so:

crm.organism:
        class: Librinfo\CRMBundle\Admin\OrganismAdmin
        arguments: [~, Librinfo\CRMBundle\Entity\Organism, LibrinfoCRMBundle:OrganismAdmin]
        tags:
            -   name: sonata.admin
                manager_type: orm
                group: Customers Relationship Management
                label_translator_strategy: blast_core.label.strategy.librinfo

You will have full control of your admin labels

Also see: SonataAdmin: replace ID in breadcrumbs