Skip to main content

Drupal 8: Theme suggestions for content type AND view mode.

I was trying to theme a specific vocabulary and a specific view mode and the suggestions, by default, didn't allow for this. After some searching I came across this article, which mentioned using the hook:

hook_theme_suggestions_HOOK_alter().

So, I added the function below in my .theme file to make the following template suggestion available: 'taxonomy_term__VOCABULARY__VIEW_MODE'

  1. /**
  2.  * Implements hook_theme_suggestions_HOOK_alter().
  3.  */
  4. function MYTHEME_theme_suggestions_taxonomy_term_alter(&$suggestions, &$vars) {
  5.   // Give a custom template suggestion for the taxonomy type and view mode.
  6.   $suggestions[] = $suggestions[0] . "__" . $vars['elements']['#view_mode'];
  7. }


Comments