admin管理员组文章数量:1355264
I have 2 content types created in Drupal 10 using Structure > Content Types > Add Content Type:
- Universal Page => universal_page
- Home Page => home_page
How do I check for content type specifically within a Paragraphs Module template like paragraph.html.twig
?
I tried {{ node.bundle }}
but that only works in node.html.twig
templates.
I want to achieve below:
{% if node.bundle == 'universal_page' %}
do X
{% else %}
do Y
{% endif %}
I have 2 content types created in Drupal 10 using Structure > Content Types > Add Content Type:
- Universal Page => universal_page
- Home Page => home_page
How do I check for content type specifically within a Paragraphs Module template like paragraph.html.twig
?
I tried {{ node.bundle }}
but that only works in node.html.twig
templates.
I want to achieve below:
{% if node.bundle == 'universal_page' %}
do X
{% else %}
do Y
{% endif %}
Share
Improve this question
edited Mar 30 at 16:37
DarkBee
15.5k8 gold badges72 silver badges117 bronze badges
asked Mar 30 at 14:57
User301276User301276
632 silver badges8 bronze badges
2
|
1 Answer
Reset to default 0This preprocess function works if it helps others:
YOURTHEME.theme:
function YOURTHEME_preprocess_paragraph(&$variables){
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
$variables['content_type'] = $node->getType();
//add if to prevent listing page error
}
paragraph.html.twig:
{{ content_type }}
{% if content_type == 'universal_page' %}
do X
{% else %}
do Y
{% endif %}
Inspired by https://createdbycocoon/knowledge/get-node-values-paragraph-templates-twig-drupal-8
本文标签: twigCheck for Content Type in paragraphhtml templateStack Overflow
版权声明:本文标题:twig - Check for Content Type in paragraph.html template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743985031a2571240.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
{{ node.bundle }}
returns an empty string. +2 upvote – User301276 Commented Mar 31 at 14:48