I have a paragraph with two field:

  • Paragraph title [text field]
  • Node [entity reference field]

The node is displaying its title and an image. By default the Node title is a H2 heading. But I want it to change based on the existence of the Paragraph title.

When the Paragraph title is set. The Paragraph title should be a H2 and the Node title should be a H3. But when the Paragraph title field is empty the Node title should be surrounded by a H2.

How can this be done?

  • There are really many questions and answers on this site about "get child paragraph from node" or "getting parent node of paragraph".
    – Hudri
    Jun 20, 2022 at 9:58

1 Answer 1


You probably can use the hook function template_preprocess_paragraph(&$variables).

In the hook function you can test if you are on a the proper paragraph and if you are on a Node object like this :

$paragraph_type = $variables['paragraph']->getType();
$node = \Drupal::routeMatch()->getParameter('node');

And then from here you can test if your Node has the value you want (the title) and if the paragraph_type is the correct type and return the good title in $variables['my_new_title'].

In twig template of the paragraph, you can then call {{ my_new_title }}.

Not the answer you're looking for? Browse other questions tagged or ask your own question.