2

I am experimenting with durpal 8 theme hook suggestions. I tried to customize the the theme suggestions based on the style name. I want the ability to add a twig template for each style if I wanted so I tried this:

function themename_theme_suggestions_image_style_alter(&$suggestions, array $variables, $hook) {
  $suggestions[] = [$hook . '__' . $variables['style_name']];
}

and

function themename_theme_suggestions_image_alter(&$suggestions, array $variables, $hook) {
  $suggestions[] = [$hook . '__' . $variables['style_name']];
}

I was expecting to see this as a theme hook suggestion in the html comments printed by in the page source after enabling twig debuging.

Using xdebug the hook is triggered but I don't find it in the suggestions list in the html after enabling twig.config.debug to true.

am I missing anything?

2
  1. hook_theme_suggestions_HOOK_alter doesn't use a third argument.
  2. You are supposed to add the suggestions as string.

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYTHEME_theme_suggestions_image_style_alter(array &$suggestions, array $variables) {
  $suggestions[] = $variables['theme_hook_original'] . '__' . $variables['style_name'];
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYTHEME_theme_suggestions_image_alter(array &$suggestions, array $variables) {
  $suggestions[] = $variables['theme_hook_original'] . '__' . $variables['style_name'];
}
| improve this answer | |
  • the $hook param is not required but it does get passed to hook_theme_suggestions_HOOK_alter . Not sure why it's not in the documentation you referenced. But it's indeed the array that I did not catch. Not sure how I missed it ... thanks – awm Dec 16 '18 at 23:58
  • I wish there was a way to come back and upvote an answer that I'd already upvoted from a while back. Thanks again for the fish! – JamesWilson Mar 27 at 16:57

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.