0

I'm using the following function to render a image field in a template, this works great but I would also like to add a custom alt attribute to the image:

function <module>_render_field($node, $field_name, $delta = 0, $display = array()) { 
   $field = field_get_items('node', $node, $field_name);
   $field = field_view_value('node', $node, $field_name, $field[$delta], $display);
   return render($field);  
}

<module>_render_field($node, 'field_image', 0, array(
  'type' => 'image',
  'settings' => array(    
     'image_style' => 'object_380x250',
     'image_link' => 'content',
   ),
));

I was trying to add the alt attribute to the image by changing the above code to:

<module>_render_field($node, 'field_image', 0, array(
  'type' => 'image',
  'settings' => array(    
     'image_style' => 'object_380x250',
     'image_link' => 'content',
   ),
      'attributes' => array(
         'alt' => 'alternate'
      )
));

The above didn't worked but I managed to add the attribute by changing the module code to:

function <module>_render_field($node, $field_name, $delta = 0, $display = array()) { 
   $field = field_get_items('node', $node, $field_name);
   $field = field_view_value('node', $node, $field_name, $field[$delta], $display);
   // add alt attribute
   $field['#item']['alt'] = 'alternate';
   return render($field);  
}

I'm hoping there is a nicer way to achive this.

2
+50

This is an ideal case for using a preprocess in your template.php. You can use a field preprocess. Something like:

function THEME_preprocess_field(&$variables) {
  // do a dpm on $variables to see what you can use.
  $variables['items']['0']['#item']['alt'] = 'alternate';
}

You can also add a template file for your field. Check theme_hook_suggestion part in template_preprocess_field function for available theme_hook_suggestions. Check this to see how you could use them.

Edit: Here is a working code I tested:

// Here I use bartik as an exmample. You can change it to your theme name.
// First we add a preprocess to a certain field. The following is ONE way to allow 
// you to create   preprocess functions for any field. There might be better ways.
function bartik_preprocess_field(&$variables) {
// this will tell our theme system to look for preprocess's of the form
// THEME_NAME_field__field_FIELD_NAME.
$function = 'bartik_preprocess_field__'. $variables['element']['#field_name'];
  if(function_exists($function)) {
    $vars = $function($variables);
  }

}
// Then we change what needs to be changed.
function bartik_preprocess_field__field_nice_pic(&$variables) {
  // krumo($variables);
  $variables['items']['0']['#item']['alt'] = '123';
  // krumo($variables);
}
2

If you look at it, you'll notice MODULE_render_field is not useful at all. Once you get the renderable array from field_view_value you can change the values in it and render it. If you're adding that to a template, it will look fugly, for sure. But it belongs in a preprocess call, where it will look just normal:

<?php

function MYTHEME_preprocess_SOME_TEMPLATE($vars) {
  $field_items = field_get_items('node', $vars['node'], $vars['field']['field_name']);
  $field_value = field_view_value('node', $vars['node'], $vars['field']['field_name'], $vars['field'][$delta], $display);
  // Alter the renderable array.
  $field_value['#item']['alt'] = 'alternate';
  $vars['altered_field'] = $field_value;
}

And in the template:

<div class="something">
 <?php print render($altered_field); ?>
</div>
  • Still I would like to set the alt attribute to different values depending on the image, perhaps to the title of the node containing the field. What would be a good approach to do this? – Cyclonecode Mar 21 '14 at 1:16
  • Easier to show you with code, can you gist it? – Capi Etheriel Mar 21 '14 at 1:28
  • What do you mean with "can you gist it" – Cyclonecode Mar 21 '14 at 1:30
  • gist.github.io – Capi Etheriel Mar 21 '14 at 20:31
  • Can't you just edit your answer and add the relevant code? – Cyclonecode Mar 22 '14 at 0:23

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.