1

I created a module which creates a content type called equipment. Which includes multiple fields and a field collection. But now I want to style the template with those fields. As suggested from the HTML comments thanks to the Twig debugging, I created a file called node--equipment.html.twig which is located inside the templates folder of the module. Unfortunately this template won't show up, so it shows the default template.

Doesn't Drupal 8 recognize the Twig file? What is the reason for not showing the wanted template? How can I fix this?

3

To recognize a suggestion in a module, you need to explicitly specify it in hook_theme() of your module.

system_theme() for example has this:

// Normally theme suggestion templates are only picked up when they are in
// themes. We explicitly define theme suggestions here so that the block
// templates in core/modules/system/templates are picked up.
'block__system_branding_block' => array(
  'render element' => 'elements',
  'base hook' => 'block',
),

Copy the original definition and then change the key and set the base hook.

  • What about the variables (fields I applied to the content type)? Do I need to specify those inside of the suggestion? – adiii4 May 15 '16 at 7:44
  • And the path of the template file inside of the module? – adiii4 May 15 '16 at 8:15
  • You need the same definition as defined in node_theme(), it use a render element, not variables. Apparently it works fine for system.module without a path, maybe it defaults to the templates folder. – Berdir May 15 '16 at 10:40
  • You are right you don't need the path. Drupal automatically looks for the template inside of the templates folder. Thanks for your help! – adiii4 May 15 '16 at 10:54
2

I finally fixed the problem. As Berdir said, to recognize a template you need to explicitly specify a suggestion in the hook_theme() implementation of the module. My hook_theme() looks like this:

function equipment_theme(array $variables) {
  $theme = array();

  # Suggestion for the template
  $theme['node__equipment'] = array(
      'render element' => 'content',
      'base hook' => 'node',
      # template file
      'template' => 'node--equipment',
      # location of the template file
     'path' => drupal_get_path('module', 'equipment') . '/templates',
  );

  return $theme;
}

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.