I'd like to add an item to a multivalued image field rendered by the Field Slideshow module. I need to display an image coming from field image B of entity B in the slideshow of field image A of entity A. The Field Slideshow module implements hook_field_formatter_view. I'd like to be able to add an item at the end of the $items variable of this function. How can I do that ?
2 Answers
If someone comes here, use template_preprocess_field
In template.php :
function MYTHEME_preprocess_field(&$variables) {
$element = $variables['element'];
if ($element['#field_name'] == 'field_myfield') {
//Load the related node, I let you do this step ...
//$nid = ...
$nodeB = node_load($nid);
$variables['items'][0]['#items'][] = $nodeB->field_another_image_field[LANGUAGE_NONE][0];
//If you have a pager
$variables['items'][0]['#pager']['#items'][] = $nodeB->field_another_image_field[LANGUAGE_NONE][0];
}
}
The hook you're looking for is hook_field_prepare_view https://api.drupal.org/api/drupal/modules%21field%21field.api.php/function/hook_field_prepare_view/7
It runs before hook_field_formatter_view().
-
I tried hook_field_prepare_view and hook_field_formatter_prepare_view. They were never triggered. I put them in my module, maybe I'll try in my template.php file. Also, I did find a workaround by using MYTHEME_ecommerce_preprocess_field Commented Sep 23, 2015 at 9:15
-
1Ah, you're right. hook_field_prepare_view() is one of those many field related hooks available only to the module that declares the field type. Another place where Drupal (pre Drupal 8) implements object oriented concepts with PHP 4 era methodology. Commented Sep 24, 2015 at 15:44