1

I want to store a template for my content-type in a module. This module provides the content-type (via features). My template "node--my-content-type.html.twig" should be in this module, too. Not in the Themes templates/ folder.

I know there are some related questions yet. I think I got the first step already; Drupal is recognizing my template and is outputing its contents.

In Drupal 7 the approach was made with something like this:

function mymodule_theme($existing, $type, $theme, $path) {
  return array(
  'node__my_content_type_machine_name' => array(
      'render element' => 'elements',
      'template' => 'templates/node--my-content-typee-machine-name',
    )
  );
}

Drupal 8 recognizes the template when I change this a bit.

'template' => 'node--my-content-type-machine-name'

So far so good. But now I am trying to render a field in my template and here I am failing.

{{ content.field_body }} ==> This is doing **nothing**

In my theme´s template-folder this is working fine. Any ideas?

3

You are missing the base hook definition. See how system_theme() does it for block templates. So, add 'base hook' => 'node' and it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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