1

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.

kowsalya v
  • 31
  • 5
  • Where did you put that code? Theme hooks like that need to be placed in the {theme_name}.theme file of your theme. You rename the function to match the name of the theme, like https://stackoverflow.com/a/32460523/6031948. I'm not familiar with what is causing the lazy loading part, but my guess is that is a configuration option that could be disabled. Maybe in your theme, or a contrib module. You priobably don't need to add the theme hook just for that. – Brian Wagner Sep 09 '21 at 14:57

1 Answers1

0

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;
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 09 '22 at 04:26