简体   繁体   中英

Drupal 9.2 template_preprocess_image hook not working

I am using drupal 9.2 latest version. For images height and width are setting automatically also

loading = 'lazy'

this attribute added in every images in website.

I tried to unset these attributes using the below hook.

function theme_preprocess_image(&$var) { 
  $var['attributes']['loading'] = 'auto';
  unset($var['height'], $var['width']);
}

But the hook is not triggered. Is there anything need to change in configuration for latest version.

I know this question is one year old but I think this information is kind of important.

You are trying to remove the height and width attributes of your images. I can imaging this is because you are setting the sizes with css.

This is problematic because your browser needs those attributes to calculate the image ratio and reserve this space inside the imagecontainer.

If you remove those attributes, no space will be reserved and the websites starts jumping around while loading. So yeah: Dont remove height and width, just overwrite it with css:

width: 100%
height: auto;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM