Nested menu items at SonataAdminBundle sidebar

4.2k views Asked by At

I would like to nested menu items like a tree. I have this configuration at the moment:

dashboard:
    groups:
        company:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Company
            items:
                - sonata.admin.company
                - sonata.admin.brand
                - sonata.admin.media
                - sonata.admin.message
        territory:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Territory
            items:
                - sonata.admin.territory
                - sonata.admin.target
        reps:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Representative
            items:
                - sonata.admin.representative
                - sonata.admin.email
                - sonata.admin.targetbrand
                - sonata.admin.territorybrand

And I would like have something like:

dashboard:
    groups:
        company:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Company
            label_catalogue: PDOneBundle
            items:
                - sonata.admin.company
                    - sonata.admin.brand
                        - sonata.admin.media
                        - sonata.admin.message
        territory:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Territory
            label_catalogue: PDOneBundle
            items:
                - sonata.admin.territory
                     - sonata.admin.target
        reps:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Representative
            label_catalogue: PDOneBundle
            items:
                - sonata.admin.representative
                    - sonata.admin.email

How I can do that? It's possible?

UPDATE

Based on the docs by SonataAdminBundle and recommended by @hugo-briand I made some changes by extending the menu so I have created a file under PDI\PDOneBundle\EventListener\MenuBuilderListener.php and looks like this:

namespace PDI\PDOneBundle\EventListener;
use Sonata\AdminBundle\Event\ConfigureMenuEvent;

class MenuBuilderListener
{
    public function createMainMenu(ConfigureMenuEvent $event)
    {
        $menu = $event->getMenu();

            $menu
                ->addChild('Dashboard', array('uri' => '/'))
                ->setAttribute('icon', 'fa fa-home');

            $menu
                ->addChild('Company', array())
                ->setAttribute('icon', 'fa fa-inbox')
                ->addChild('Company', array(
                   'route' => 'admin_pdi_pdone_company_list',
                ))
                ->setAttribute('icon', 'fa fa-inbox')
                ->getParent()
                ->addChild('Brand', array(
                    'route' => 'admin_pdi_pdone_brand_list',
                ))
                ->setAttribute('icon', 'fa fa-inbox')
                ->addChild('Media', array(
                    'route' => 'admin_pdi_pdone_media_list',
                ))
                ->setAttribute('icon', 'fa fa-inbox')
                ->getParent();
    }
}

Then I registered the listener at PDI\PDOneBundle\Resources\config\services.yml as follow:

services:
    app.menu_listener:
        class: PDI\PDOneBundle\EventListener\MenuBuilderListener
        tags:
            - { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: createMainMenu }

But is not working since the same default menu still showing. Any advice?

1

There are 1 answers

5
Hugo Briand On BEST ANSWER

Yes, you can. But not precisely as you specified it.

SonataAdmin now integrates KnpMenu to generate the side menu. It is documented here (https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_knp_menu.html); take care to be on the master version though, this has not yet been released as a stable version.

Note: As the documentation has not been republished yet, some of the information is missing. You can see it directly on the Github repository though: https://github.com/sonata-project/SonataAdminBundle/blob/41328b42f91bf4c72153cdb1fc2c50a5092c7755/Resources/doc/cookbook/recipe_knp_menu.rst

For each item in your menu, you can now provide a KnpMenu provider that will generate your menu, and override the layout associated.