0

I created a module implementing a content type. How can I set a custom template file for that content type and for the teaser and full view modes? I need to use the template files in the folder of my module, not the theme the site is currently using.

These are some details about my module.

  • Module name: My Module
  • Content type name: My Content
  • Teaser template name: node--my-content--teaser.html.twig
  • Full template name: node--my-content--full.html.twig
  • Field template name: field--node--field-my-field--my-content.html
1

Nodes

Pattern: node--[type|nodeid]--[viewmode].html.twig

Base template: node.html.twig (base location: core/modules/node/templates/node.html.twig)

Theme hook suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:

  1. node--nodeid--viewmode.html.twig
  2. node--nodeid.html.twig
  3. node--type--viewmode.html.twig
  4. node--type.html.twig
  5. node--viewmode.html.twig
  6. node.html.twig

Note that underscores in a content type's machine name are replaced by hyphens.

Source: Twig Template naming conventions

In your case this is number 3.

Example:

node--article--teaser.html.twig

If you want to place the template inside the module see: Template for custom content type

In your case this would look like this:

/**
 * Implements hook_theme().
 */
function my_module_theme() {
  return [
    'node__my_content__teaser' => [
      'base hook' => 'node',
    ],
    'node__my_content__full' => [
      'base hook' => 'node',
    ],
  ];

In my experience you only need base hook. Drupal will discover the correct template name and path on its own. You have to be careful with the underscores (_) in the theme hook, they should match exactly the hyphens (-) of the template name.

  • Thanks for your help and answer, but I need to use the template files inside of the folder of my module, not the theme the site is currently using, I read your suggested link, but I need more comprehensive approach for this problem.Can you help me, please? – Mojtaba Reyhani Sep 5 '16 at 13:10
  • I added an example. – 4k4 Sep 5 '16 at 13:46
  • Great and perfect solution, A world of thanks for your kindness, and in the following is there any solution for Twig field templates in this case? – Mojtaba Reyhani Sep 5 '16 at 14:16
  • Replace node with field. You can add this to the array returned from hook_theme. – 4k4 Sep 5 '16 at 15:49
  • Ok, you're right, my problem was solved, Thanks for everything. – Mojtaba Reyhani Sep 5 '16 at 16:08
0

You don't need to do anything, except creating the necessary template files for the theme you are using, with the correct names. The Node module already suggests the following template files, for a node, in node_theme_suggestions_node().

  $suggestions[] = 'node__' . $sanitized_view_mode;
  $suggestions[] = 'node__' . $node->bundle();
  $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'node__' . $node->id();
  $suggestions[] = 'node__' . $node->id() . '__' . $sanitized_view_mode;

Supposing that custom_type is the content type (i.e. bundle) created from the module, you could use node--custom-type--teaser.html.twig as template file for the teaser of that content type, and node--custom-type--full.html.twig for the full display mode of the same content type.

Keep in mind that template files are searched following a specific order; the first existing template file is then used. So, if you are viewing the teaser of a node whose ID is equal to 2, the following template files will be used, if they exist.

  • node--2--teaser.html.twig
  • node--2.html.twig

In this case, you could have a node--custom-type--teaser.html.twig file, but it will never be used.

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.