简体   繁体   English

Drupal 8 Twig模板-如果节点具有菜单项,则显示菜单

[英]Drupal 8 Twig Template - Show menu if node has a menu item

I am creating custom twig templates for a Drupal 8 theme. 我正在为Drupal 8主题创建自定义树枝模板。 I want to render a menu in a region only if the node has a menu link (ie when editing the node the menu settings on the right has been configured so the “provide a menu link” box has been checked). 我只想在节点具有菜单链接的情况下才在区域中渲染菜单(即,在编辑节点时,已经配置了右侧的菜单设置,因此已选中“提供菜单链接”框)。

Ideally I would like to create this in the twig template so I can change the layout accordingly, but any pointers greatly appreciated! 理想情况下,我想在树枝模板中创建它,以便我可以相应地更改布局,但是任何指针都非常感谢!

Usually this issue is handled differently(by creating a separate content type and set visibility) but for the sake of the question I'll give some solutions. 通常,此问题的处理方式不同(通过创建单独的内容类型并设置可见性),但是出于问题的考虑,我将提供一些解决方案。

Solution 1: Block Visibility Groups. 解决方案1:阻止可见性组。

If that is not enough. 如果那还不够。

Solution 2: hook_block_view_alter(or any other hook_ENTITY_TYPE_view_alter) where you can deactivate the access to menu block after you check if condition is met with Drupal::routeMatch() to check what node is displayed. 解决方案2:hook_block_view_alter(或任何其他hook_ENTITY_TYPE_view_alter),在您检查Drupal :: routeMatch()是否满足条件以检查显示的节点后,可以在其中停用对菜单块的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM