2

I'm trying to add custom class to an image.

The image is generated with a image field called "field_hero" so i'm trying to add a class to this image that is generated from that specific field.

Right now, I've tried with :

function mytheme_preprocess_image(&$variables) {
  $element = $variables['element'];
  if (isset($element['#field_name'])) {
    if ($element['#field_name'] == 'field_hero') {
       $variables['attributes']['class'][] = 'custom-class';
    }
  }   
}

I can actually add a class with $variables['attributes']['class'][]

But with the if statements it doesn't work because it doesn't go through.

How can i make it work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.