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
{{ node.bundle }}
returns an empty string. +2 upvote – User301276 Commented Mar 31 at 14:48