admin管理员组

文章数量:1355264

I have 2 content types created in Drupal 10 using Structure > Content Types > Add Content Type:

  1. Universal Page => universal_page
  2. 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:

  1. Universal Page => universal_page
  2. 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
  • "I tried {{ node.bundle }}" - Please elaborate - What didn't work? Did you receive an error? – DarkBee Commented Mar 30 at 16:36
  • {{ node.bundle }} returns an empty string. +2 upvote – User301276 Commented Mar 31 at 14:48
Add a comment  | 

1 Answer 1

Reset to default 0

This 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