Skip to main content
Category:

in this tuto, I'll show you how to add css classes into a specific image field in Drupal 8.

Add this code to your theme's .theme or your module function:

/**
 * @param $variables
 */
function  mymodule_preprocess_field(&$variables)
{
  if ($variables['element']['#field_name'] == 'field_name') {
    foreach (array_keys($variables['items']) as $index) {
      $variables['items'][$index]['content']['#item_attributes']['class'][] = 'my-img-class';
    }
  }
}

 

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook