phpsymfony4syliusknpmenubundle

Sylius: how to customize to User-menu in top right bar?


As described in the official Docs, i was able to customize both the Admin-Dashboard menu and also the Menu in the account section for a user.

However, how can i customize the menu, that is shown in upper top right bar including menu items like "My account" and "Logout"? I want to add some items here.

This is what i mean:

enter image description here

What i have done so far:

First i have defined the MenuBuilder-Customization in services.yml:

app.listener.shop.menu_builder:
    class: AppBundle\Menu\AccountMenuListener
    tags:
        - { name: kernel.event_listener, event: sylius.menu.shop.account, method: addAccountMenuItems }
        - { name: kernel.event_listener, event: sylius.shop.menu_builder.account, method: addTopMenuMenuItems }
        - { name: kernel.event_listener, event: sylius.menu_builder.frontend.main, method: addTopMenuMenuItems }

And this is my customized MenuBuilder:

<?php

namespace AppBundle\Menu;

use Knp\Menu\MenuItem;
use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent;

final class AccountMenuListener
{
    /**
     * @param MenuBuilderEvent $event
     */
    public function addAccountMenuItems(MenuBuilderEvent $event): void
    {
        $menu = $event->getMenu();

        $menu->removeChild('address_book');
        $menu->removeChild('order_history');

        $menu
            ->addChild('new', ['route' => 'sylius_shop_homepage'])
            ->setLabel('My Reservations')
            ->setLabelAttribute('icon', 'star');
    }

    public function addTopMenuMenuItems(MenuBuilderEvent $event): void
    {
        $menu = $event->getMenu();

        // list all items
        foreach($menu->getChildren() as $child) {
            echo $child->getName() . '<br>';
        }
        die;

    }
}

At least for the Side-Menu that appears in "my account" section, everything worked well. But the top-right-menu can't get reached this way. Whether "sylius.menu_builder.frontend.main" nor "sylius.shop.menu_builder.account" will catch here.

How can it be done properly?


Solution

  • There are two ways:

    1) you should override these lines: https://github.com/Sylius/Sylius/blob/1.2/src/Sylius/Bundle/ShopBundle/Resources/views/layout.html.twig#L33 .

    2) Or you can override block top (1 line above) inyour custom twig template, which inherits that mentioned template.