1

I am having trouble adding a class to an image using a preprocessor function in Drupal 9. Specifically, I only want certain images to have that class added. The image is being added using paragraphs.

I've tried a few variations of the following and cannot seem to get class added. I've also tried using ['#item'] but it causes an error.

function confluence_preprocess_field__paragraph__field_card_image(&$variables) {    
    foreach($variables['items'] as $key => $item){
        $variables['items'][ $key ]['content']['item']['class'] = 'card-img-top';
    }
}
0

1 Answer 1

0

You can add it via the content's #item_attributes array like so:

foreach($variables['items'] as $key => $item){
  $variables['items'][$key]['content']['#item_attributes']['class'][] = 'card-img-top';
}
1
  • Fantastic, it works. Thank you so much. Commented Nov 22, 2021 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.