Using theme_preprocess_node i'm trying to add a class to all images on the page

Drupal Answers Asked by TopTomato on December 31, 2021

My code below is not working and i’m not sure why. I’ve tried a few variants.
according to the KPR variables output, the image I’m trying to access is here.


i think the 0 signifies it is the first (and only in this case) image on the page w/ the machine field name ‘field_about_image’. so i wrote my code as:

function myTheme_preprocess_node(&$variables){
    if($variables['type']=='basic_page_with_pic') {
echo '<script>alert("my func running")</script>'; //Verified it's working
    $variables['field_about_image'][0]['class'] = 'myclass;

i’ve also tried this, just to get a class attribute added

$variables['field_about_image'][0][] = 'class';

also the title attribute comes up as empty. so just as an execrcise i tried this.

$variables['field_about_image'][0]['title'] = 'some title;

any help on how to do this would be greatly appreciated!

One Answer

From what I can tell, hook_preprocess_node() won't let you add both classes and title attributes to images from image fields. Try using hook_preprocess_field() as in the example below:

function myTheme_preprocess_field(&$variables) {
  if (($variables['element']['#field_name'] == 'field_about_image') && ($variables['element']['#bundle'] == 'basic_page_with_pic')) {
    $variables['items'][0]['#item']['title'] = t('This is the title text that will display when hovering.');
    $variables['items'][0]['#item']['attributes']['class'][] = 'class1';
    $variables['items'][0]['#item']['attributes']['class'][] = 'class2';

Be sure to clear your cache.

Answered by Bryan Jiencke on December 31, 2021

Add your own answers!

Related Questions

Solr index fields not showing a field

1  Asked on January 12, 2021 by heather


Programmatically get product name by using order variable

2  Asked on January 7, 2021 by selva


Applying patch on core module failing

0  Asked on January 6, 2021 by nabil


Custom module is not working

1  Asked on December 30, 2020 by ts-guhan


Unlimited field collection pagination in node edit page

1  Asked on December 30, 2020 by dmk-i-t


Error: Private files directory not fully protected

2  Asked on December 28, 2020 by glenviewjeff


How to add Autocomplete to a Views Custom Textfield Filter

1  Asked on December 27, 2020 by laceysanderson


Cannot disable or replace logo

1  Asked on December 25, 2020 by digital-fire


Webform onchange added with hook_form_id_alter

1  Asked on December 25, 2020 by richard-luckhurst


Form API placeholder attribute rendered as “- Select -“

1  Asked on December 24, 2020 by hotwebmatter


Adding a dependencies in theme info file

1  Asked on December 23, 2020 by aryashree-pritikrishna


Javascript in Drupal

1  Asked on December 23, 2020 by zephirus


Titles not showing for tabs pages

2  Asked on December 21, 2020 by andyw


Can’t connect to remote DigitalOcean database

1  Asked on December 18, 2020 by ymdahi


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP