Pantheon Community

Drupal 8: Alter all nodes of a content type

1 Like

You should be able to us the node.html.twig template and override per content type by naming it: node--content-type-machine-name.html.twig and placing that in your templates directory.

If your content is static, you can simply place the markup in that file above the {{content}} variable in twig.

If it’s dynamic, you can use the template_preprocess_node function to add variables based on fields/etc and then use those variables in the twig file:

function MYTHEMENAME_preprocess_node(&$variables) {
  $node = $variables['elements']['#node'];
  $bundle = $node->bundle();
  if ($bundle == 'content_type_machine_name') {
    $variables['twig_variable_name'] = "Hello world!"; // Put dynamic data here :)

In twig:

{{ twig_variable_name }}

Hope the helps!