0

I want to override the template for 5 content type that they have all the same attributes, but a different name ,so the suggestion is :

node--contenttype1.html.twig 
node--contenttype2.html.twig 
node--contenttype3.html.twig 
node--contenttype4.html.twig 
node--contenttype5.html.twig 

how can i implement a same override for all those 5 content type ?

1

1 Answer 1

1

Normally you'd simply use node.html.twig and then have different node templates for other node types. But I can imagine that it can make sense to bundle certain node types differently instead of providing distinctive templates for all other node types.

So you can implement hook_theme_suggestions_alter or hook_theme_suggestions_HOOK_alter to provide an extra template suggestion. The following snippet lets nodes of a given type pick up a node--foobar.html.twig template if present.

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYTHEME_theme_suggestions_node_alter(array &$suggestions, array $variables) {

  $bundles = [
    'contenttype1',
    'contenttype2',
    'contenttype3',
    'contenttype4',
    'contenttype5',
  ];

  /** @var \Drupal\node\Entity\Node $node */
  $node = $variables['elements']['#node'];

  if (in_array($node->bundle(), $bundles)) {
    $suggestions[] = 'node__foobar';
    $suggestions[] = 'node__foobar__' . $variables['elements']['#view_mode'];
  }
}

Your Answer

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

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