dongtu4028 2018-08-10 08:54
浏览 36
已采纳

Sylius:如何自定义右上角的用户菜单?

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?

  • 写回答

1条回答 默认 最新

  • douganmo1121 2018-08-10 11:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面