Reputation: 615
Is there an easy way to order the sonata admin menu with a custom order?
This is my actual menu:
And for example I want the next order:
Upvotes: 0
Views: 649
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);
}
}
services: app.menu_listener: class: App\EventListener\MenuBuilderListener tags: - { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: addMenuItems }
Upvotes: 0
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