Score:0

hook_theme_suggestions_image_alter() not returning ['data-responsive-image-style']

ru flag

Perhaps I have similar issue to this Drupal 9.2 template_preprocess_image hook not working.

When I put this code in myTheme.theme

function HOOK_theme_suggestions_image_alter(array &$suggestions, array $variables)
{
 $responsive_style = $variables['attributes']['data-responsive-image-style'];
 $suggestions[] = 'image__'.$responsive_style;
}

The hook works good on inline images with this module Inline responsive images and it returns ['data-responsive-image-style'].

But it doesn't work with regular article images, it returns null.

When I debug $variables['attributes'] it has just this value

$variables[...] array (1)
  property => array (1)
    => string (12) "schema:image"
No Sssweat avatar
ua flag
I use that module. The `data-responsive-image-style` is only added to text editor images only, it's not designed to work with image fields. For image fields you need to use core's responsive images module.
apaderno avatar
us flag
@NoSssweat That sounds like an answer.
Drazen avatar
ru flag
I know that Inline responsive images module is used only for text editor, but it works fine. and return correct data $variables['attributes']['data-responsive-image-style']. I'm using core module for responsive images but in upper case it returns null.
Score:0
ru flag

I have found solution. I invoked from parent twig responsive_image_style_id

function hook_preprocess(&$variables, $hook) {
  if ($hook == 'responsive_image') {
    $GLOBALS['responsive_image_style_id'] = $variables['responsive_image_style_id'];
  }
}

function hook_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if ($hook == 'image') {
    return $suggestions[] = 'image__'.$GLOBALS['responsive_image_style_id'];
  }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.