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.
$variables[‘field_about_image’][0]
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.
Answered by Bryan Jiencke on December 31, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP