1

I can use a template preprocess function for node to simply get the alt tag of an image field image using the venerable field_get_items as such:

function MYTHEME_preprocess_node(&$vars, $hook) {
// Global node.
  $node = $vars['node'];

 $field_image_items = field_get_items('node', $node, 'field_image');
  // Load individual data from 'field_image'.
    $vars['alt_caption'] = $field_image_items[0]['alt'];
}

Then in node.tpl.php, I can render the alt tag of the image from the variable we created above as:

<?php print $alt_caption; ?>

... and that works great.

However, I'd like to customize the template for the image itself so when I create a field--field_image.tpl.php template, sure enough the image renders fine but if I add the $alt_caption variable, that does not render. I'm guessing this will simply not work or there is an extra step I need to do for the variable to render in this new template.

1

You need TEMPLATE_preprocess_field() function at template.php

You can use this preprocess function in same way as you are doing for NODE. Like -

function MYTHEME_preprocess_field(&$vars) {
  if ($node = menu_get_object()){
    $node = $vars['node'];
    $field_image_items = field_get_items('node', $node, 'field_image');
    $vars['alt_caption'] = $field_image_items[0]['alt'];
  }
}

Then go to your TPL made for your particular field and print it where ever you like just like you have mentioned --

<?php print $alt_caption; ?>

And it will work like charm!! I have implemented like this several times.

But yeah...don't forget to clear your cache after this implementation.

  • I'm getting a nasty error with that even after clearing cache: Notice: Undefined index: node in gratis_preprocess_field() (line 266 of template.php). EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7663 of /includes/common.inc). -- It seems preprocess field does not like the node variable defined? (Note gratis is the name of my theme). – Danny Englander Jun 25 '13 at 16:02
  • @DannyEnglander - Edited my answer after seeing my own template.php, please check now after updating your code :) Otherwise there are other option to get relevant info of field by the machine name of field as it's available in $vars at TEMPLATE_preprocess_field() – RajeevK Jun 25 '13 at 16:06
  • Ok this now works but without $node = $vars['node'];, that still throws the error but if I take it out, it works well on a node page but not on my main page with teasers. There, I'm getting this error: Notice: Undefined variable: alt_caption in include() (line 54 of field--field_image.tpl.php). So not sure if this will end up being a good solution unless there is a way to make the variable available in the teaser? – Danny Englander Jun 25 '13 at 16:19
  • @DannyEnglander - Because it expect menu_get_object() to get executed which take place on full node load. Can you please output $vars inside this preprocess function by doing dsm($vars) & give me the result ? – RajeevK Jun 25 '13 at 16:24
  • @DannyEnglander - You can chat with me on IRC, I am in room #drupal & #drupal-support with name RajeevK – RajeevK Jun 25 '13 at 16:33
0

As I was having issues with this working in teaser mode, another way to do this is to forgo the preprocess function altogether and render this in your custom image field template using the #item var that's already available:

 <?php if ($item['#item']['alt']): ?>
    <figcaption><?php print $item['#item']['alt']; ?></figcaption>
 <?php endif; ?>

This will render a nice HTML5 caption under the image based on your alt tag. What's interesting about this is that the default field.tpl.php template in D7 does not give item as a possible variable. Inspiration for this method here.

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.