wemea/sylius-menu-plugin

插件允许在Sylius后台管理中管理店铺菜单

安装次数: 1,045

依赖者: 0

建议者: 0

安全: 0

星标: 3

关注者: 1

分支: 0

开放问题: 2

类型:sylius-plugin


README

License CI - Build Version Total Downloads

Sylius菜单插件

插件允许从后台管理中管理商店菜单

功能

  • 使用命令创建/删除菜单
    • wemea:menu:create <code>
    • wemea:menu:delete <code>
  • 管理菜单的可见性
  • 管理菜单项(链接、标题、描述、顺序/优先级、图标)
  • 默认链接类型
    • 自定义:将任何链接放入其中
    • 产品:选择链接到产品,路径会动态生成
    • 分类:选择链接到分类,路径会动态生成

Menu render in Shop page

Administration of menus

Administration of menu

Administration of menu items

Administration of menu item

摘要

  1. 安装
  2. 使用
    1. 添加新菜单
    2. 更改默认路由映射
    3. 添加自定义资源链接
  3. 使用菜单样本

安装

  1. 使用Composer安装插件

    composer require wemea/sylius-menu-plugin
  2. 如果您尚未自动添加,请将以下内容添加到您的 config/bundles.php

    Wemea\SyliusMenuPlugin\WemeaSyliusMenuPlugin::class => ['all' => true],
  3. 在您的 config/packages/_sylius.yaml 中导入配置

    imports:
        [...]
        - { resource: "@WemeaSyliusMenuPlugin/Resources/config/app/config.yml" }
  4. 在您的 config/routes.yml 中导入路由

     wemea_menu_routing:
       resource: "@WemeaSyliusMenuPlugin/Resources/config/routing.yml"
  5. 将菜单添加到您的模板中。有关更多信息,请参阅使用 - 添加菜单

  6. 运行集成测试

    ⚠️ 仅针对管理员部分有集成测试。 应该添加您自己的商店部分的测试。

使用

添加菜单

技巧 :

只能使用Symfony命令创建和删除菜单,以避免商店管理员意外删除菜单并阻止UI。在BO中可以禁用菜单。

  1. 创建新菜单

    运行 bin/console wemea:menu:create <your_menu_code>

    您可以通过运行 bin/console wemea:menu:create --help 来查看可用选项

  2. 在您的模板中添加此菜单:在您想引入菜单的页面(s)中添加此内容

        {{ render(controller('wemea_sylius_menu.controller.render_menu::renderAction', {
               'code': '<your_menu_code>'
        })) }}

    默认情况下,菜单使用 @WemeaSyliusMenuPlugin/Shop/Menu/_default.html.twig。如果您想使用自己的模板,请在渲染时添加模板选项

            {{ render(controller('wemea_sylius_menu.controller.render_menu::renderAction', {
                   'code': '<your_menu_code>',
                    'template': '@App/your/menu/template.html.twig'
            })) }}
  3. 添加behat测试以检查菜单集成

    您可以在此文件中查看完整的集成,并在此文件夹中测试

更改默认资源路由映射

如果您更改了应用程序的默认路由,则需要更改链接资源的路由映射。您可以在 config/packages/wemea_sylius_menu.yml 中使用此配置进行操作

## Default configuration
wemea_sylius_menu:
  resource_path_resolver_configuration:

    custom:
      # route is null for custom type because is not use the router resolver 
      route: null 
      parameters: []

    product:
      route: 'sylius_shop_product_show'
      parameters:
        slug: 'getSlug'
    
    taxon:
      route: 'sylius_shop_product_index'
      parameters:
        slug: 'getSlug'

参数节点是路由参数和访问资源属性的方法名称之间的关联数组

向另一个资源添加新链接类型

此插件允许创建新的资源链接。您可能需要它来重定向到自定义资源(例如品牌,我们使用此示例进行说明)

  1. 覆盖默认MenuLink实体

    <?php
    
    declare(strict_types=1);
    
    namespace App\Entity;
    use Doctrine\ORM\Mapping as ORM;
    use Wemea\SyliusMenuPlugin\Entity\MenuLink as BaseMenuLink;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="wemea_menu_link")
     */
    class MenuLink extends BaseMenuLink
    {
        //Add your new property
        /** 
         * @var Brand|null $brand 
         * @ORM\ManyToOne(targetEntity="Brand")
         * @ORM\JoinColumn(name="brand_id", referencedColumnName="id")
         */
        protected $brand;
    
        //add this property name to know properties
        public static function getLinkProperties() : array{
            return array_merge(
                parent::getLinkProperties(),
                ['brand'] // your property name
            ); 
        }
    }
  2. config/packages/_sylius.yml 中将此实体声明为资源模型

    sylius_resource:
      resources:
         wemea_sylius_menu.menu_link:
              classes:
                model: App\Entity\MenuLink
  3. Wemea\SyliusMenuPlugin\Form\Type\MenuLinkType 添加表单扩展(自动完成?)选择类型。检查官方文档以创建表单扩展。

    此字段不应该被映射

           $builder
               ->add('brand', BrandAutocompleteChoiceType::class, [
                           'mapped' => false,
               ]);

    此新字段将自动添加到模板中。默认情况下,它使用: {{ form_row(form.brand) }}

    如果您想使用自定义模板,您可以创建 templates/bundles/WemeaSyliusMenuPlugin/Admin/MenuLink/Form/Fields/_<field_name>.html.twig 并使用具有属性 form 的字段。

  4. config/packages/wemea_sylius_menu.yml 文件中添加路由配置

    wemea_sylius_menu:
      resource_path_resolver_configuration:
         # Use property name as key
         brand:
          route: 'app_shop_brand_show' 
          parameters: [
           'code' => 'getCode'
           ]
  5. 清除缓存并进行迁移以更新数据库模式

使用菜单样本

如果您需要程序化加载菜单,可以使用如下菜单固定数据

sylius_fixtures:
  suites:

    <suite_name>:
      fixtures:

        menu:
          name: menu
          options:
            custom:

              -   code: <menu_code>
                  visibility: public  # use private or public value (is public by default)
                  enabled: true       # menu is enabled by default
                  
                  #Transltion accept associative array of [locale => [title => <expected title> ]] 
                  translations:
                    en_US:
                      title: The title
                  
                  #channels is array of channels code
                  channels: ['FASHION_WEB']
                  
                  #Define items of menu
                  items:
                    - target: _self # target accept _self or _blank. By default is _self
                      
                      #Transaltions is defined like menu translation with 'description' optional node
                      translations: 
                        en_US:
                          title: <title>
                          description: <description>
          
                      # link node allow to defined the target resource of menu item 
                      link:
              
                        # for a custom link, use associative array of locale => path
                        custom_link:
                          en_US: /the/expected/path
                        
                        # for a product link use the code of target product
                        product_code: <product_code>
                        
                        # for a taxon link use the code of target product
                        taxon_code: <taxon_code>

要查看完整的实现示例,可以检查文件 footer_help_menu.yaml

问题/待完成工作

  • 为菜单项图片添加固定数据
  • 添加将锚点作为自定义链接的功能
  • 在商店使用本地化URL的情况下,管理自定义链接的当前语言环境的路径解析