如何修改Sylius主题footer事件以隐藏plus区块?
你遇到的问题是因为Sylius的UI事件配置采用合并机制——你在config/packages/sylius_ui.yaml里复制配置并注释plus区块,只是没有新增该区块,但vendor里的原始配置依然生效,所以模板还是会被渲染。下面是几个可行的解决方法:
方法一:直接禁用目标区块(最简单)
不需要复制整个事件的所有配置,只需要在config/packages/sylius_ui.yaml里单独禁用plus区块:
sylius_ui: events: sylius.shop.layout.footer.grid: blocks: sylius.shop.footer.grid.plus: enabled: false
配置生效后,该区块会被直接跳过渲染。
方法二:完全自定义Footer Grid模板
如果需要对Footer Grid做更多自定义修改,直接覆盖官方模板:
- 在你的主题目录下创建路径:
templates/bundles/SyliusShopBundle/Layout/Footer/Grid/ - 复制vendor里的原模板文件
vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/Resources/views/Layout/Footer/Grid/_grid.html.twig到刚才创建的目录 - 编辑新模板,删除掉渲染plus区块的代码(比如找到
{{ sylius_ui_render_event('sylius.shop.layout.footer.grid.plus') }}或对应的区块调用部分并删除)
方法三:用事件订阅器移除区块(灵活可控)
如果需要根据逻辑动态控制区块显示,用事件订阅器实现:
- 创建订阅器类:
// src/EventSubscriber/FooterGridBlockSubscriber.php namespace App\EventSubscriber; use Sylius\Bundle\UiBundle\Event\BlockEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class FooterGridBlockSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ 'sylius.shop.layout.footer.grid' => 'removePlusBlock', ]; } public function removePlusBlock(BlockEvent $event): void { $event->removeBlock('sylius.shop.footer.grid.plus'); } }
- 确保订阅器被Symfony识别:
自动注册的话,只要实现了EventSubscriberInterface就会生效;手动注册的话,在config/services.yaml里添加:
services: App\EventSubscriber\FooterGridBlockSubscriber: tags: - { name: kernel.event_subscriber }
内容的提问来源于stack exchange,提问作者Petr Uttendorfsky
相关产品推荐
相关产品推荐

