10

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.

cezar
  • 11,616
  • 6
  • 48
  • 84
  • If you just want to modify the translations of labels, not their structure, you can override the `sonata_breadcrumb` block within the `SonataAdminBundle::standard_layout.html.twig` template. Put the correct translation domain for the `label` var, and translate your labels in it. – Theo Pnv Dec 23 '16 at 10:49
  • mmm i'm a newbie of sonata, do you have an example? – Andrea G. Pigliafreddo Dec 23 '16 at 12:34
  • ps. i don't want translate the "List" but "Test Product" the correct breacrumb is Dashboard / Product / Product List OR Dashboard / Product List / Product List – Andrea G. Pigliafreddo Dec 23 '16 at 12:36

2 Answers2

14

Try to override classNameLabel property in your admin class:

// in your ProductAdmin class
public function configure()
{
    $this->classnameLabel = "Products";
}
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
Yehor
  • 6,203
  • 3
  • 20
  • 28
  • 5
    You don't need to have this in the configure method, just add the protected var to the top of your ProductAdmin class: `protected $classnameLabel = 'Products';` – lopsided Mar 21 '17 at 10:10
  • 1
    @AndreaG.Pigliafreddo You should mark this answer if it is correct for you. – Zuhayer Tahir May 25 '17 at 11:51
  • @lopsided Thanks! It's much shorter and nicer. I think it results in cleaner code. You should have made it as an answer. Nevertheless thanks to Ekonoval for the valuable information. – cezar Mar 05 '19 at 15:06
0

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

Community
  • 1
  • 1
Mawcel
  • 1,967
  • 15
  • 22