SensacionRC
SensacionRC

Reputation: 615

KNPMenu custom order

Is there an easy way to order the sonata admin menu with a custom order?

This is my actual menu:

enter image description here

And for example I want the next order:

Upvotes: 0

Views: 649

Answers (2)

Full-Stack Developer
Full-Stack Developer

Reputation: 438

You can find a response https://docs.sonata-project.org/projects/SonataAdminBundle/en/4.x/cookbook/recipe_knp_menu/

// src/EventListener/MenuBuilderListener.php

namespace App\EventListener;

use Sonata\AdminBundle\Event\ConfigureMenuEvent;

final class MenuBuilderListener { public function addMenuItems(ConfigureMenuEvent $event): void { $customWeeklyOrder = [ "Item that is seconds but i want first", "Item that is first but i want second", ];

    $menu = $event->getMenu();

    $weeklyMenu = $menu->getChildren()['My group'];

    $weeklyChildren = $weeklyMenu->getChildren();

    usort($weeklyChildren, function ($a, $b) use ($customWeeklyOrder) {
        $aIndex = array_search($a->getName(), $customWeeklyOrder);
        $bIndex = array_search($b->getName(), $customWeeklyOrder);

        return $aIndex - $bIndex;
    });


    $weeklyMenu->setChildren($weeklyChildren);

    
}

}

config/services.yaml

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

Upvotes: 0

Theo Pnv
Theo Pnv

Reputation: 432

The order of the menu items is defined by your admin config file, under the entry sonata_admin.dashboard.groups. It's the same as the order of your items on the dashboard. Here is the doc

If you want to go further in customizing the menu, you can override the knp builder by making a menu listener, in which you'll be able to choose the order of your items too. It is well explained in the Knp doc.

Upvotes: 3

Related Questions