2

Following the instructions on how to create a custom template for taxonomy terms I managed to add a template suggestion for a particular view mode of the taxonomy term entity by using hook_theme_suggestions_HOOK_alter:

function linkup_theme_suggestions_taxonomy_term_alter(&$suggestions, $vars, $hook) {
  if($vars['elements']['#view_mode'] == 'teaser') {
    $suggestions[] = 'taxonomy-term--teaser';
  }
}

(linkup is the name of my custom theme)

This seems to work as I can see the template suggestions when I print the twig debug information:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'taxonomy_term' -->
<!-- FILE NAME SUGGESTIONS:
   * taxonomy-term--teaser.html.twig
   * taxonomy-term--15.html.twig
   * taxonomy-term--group.html.twig
   x taxonomy-term.html.twig
-->
<!-- BEGIN OUTPUT from 'core/themes/stable/templates/content/taxonomy-term.html.twig' -->

But for some reason Drupal does not pick up the template which is located in themes/linkup/templates/taxonomy-term--teaser.html.twig

I think the naming is correct (unlike Drupal 7 in 8 you dont need to fiddle with underscores to match dashes in file names)

If I add a template called taxonomy-term.html.twig to the theme then that is discovered.

Am I missing some of Drupal 8's logic to discover the custom template?

  • How does your hook_theme() look like? – Maouna Oct 14 '16 at 13:06
  • have you rebuilt the caches ? – rémy Oct 14 '16 at 13:13
  • I do not have a hook_theme() ... & sure I rebuilt the caches ;) – batigolix Oct 15 '16 at 19:26
4

You need to use underscores in your suggestion (but continue to use dashes in your template file name):

/**
 * Implements hook_theme_suggestions_HOOK_alter() for taxonomies.
 *
 * {@inheritdoc}
 */
function HOOK_theme_suggestions_taxonomy_term_alter(&$suggestions, $vars, $hook) {
  if($vars['elements']['#view_mode'] == 'teaser') {
    $suggestions[] = 'taxonomy_term__teaser'; // note underscores
  }
}

you could also leave out your condition and do:

/**
 * Implements hook_theme_suggestions_HOOK_alter() for taxonomies.
 *
 * {@inheritdoc}
 */
function HOOK_theme_suggestions_taxonomy_term_alter(&$suggestions, $vars, $hook) {      
  $suggestions[] = 'taxonomy_term__' . $vars['elements']['#view_mode'];
}

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.