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!


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.

|improve this answer|||||
  • thank you. the title worked but adding the class did not. i tried a few diff things to get it to work like, $variables['items'][0]['#attributes']['class'][] = 'about_main_pic'; hmmmm.....? – TopTomato Jun 8 '14 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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