I have 2 content types created in Drupal 10 using Structure > Content Types > Add Content Type:
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 %}
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.com/knowledge/get-node-values-paragraph-templates-twig-drupal-8