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';




Add new comment

Restricted HTML


Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.


Contact Us

All the content is FREE but I still need your help


Become a patreon