6

I have two content type using same paragraph as field , though template code will be according to content type. How can i can have different template names for paragraph template depending on content type.

Current naming convention: paragraph--paragraph-type.html.twig

Desired naming convention: paragraph--parent-content-type--paragraph-type.html.twig

Another solution will be get the url and pass it as variable in template and use template code according to url passed.

2 Answers 2

9

Add a theme name suggestion:

mytheme.theme:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function mytheme_theme_suggestions_paragraph_alter(&$suggestions, $variables) {
  $paragraph = $variables['elements']['#paragraph'];
  $parent = $paragraph->getParentEntity();
  if ($parent) {
    $suggestions[] = 'paragraph__' . $parent->bundle() . '__' .  $paragraph->bundle();
  }
}
1

Use different view modes for different node types. On Strcuture > Paragraphs types, add as many different view modes for your paragraph, as needed by your node types. On Structure > Content types, change the display mode for the node's paragraphs field to "Rendered entity" and use the cog wheel to select the previously created, corresponding view mode. Now create template files like paragraph--paragraph-type--viewmode-name.html.twig

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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