Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

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


Page Facebook