Example: Altering the structure of field collection items

Last updated on
30 April 2025

For the purpose of displaying the links from the field collection view field formatter at the top instead of the bottom here a little example:

<?php
function template_preprocess_field(&$variables) {
  if ($variables['element']['#field_type'] == 'field_collection') {
    //Iterate through all field items
    foreach ($variables['items'] as $key => $item) {
      $links = $item['links'];
      unset($item['links']);
      //Prepend links to render array
      $variables['items'][$key] = array_merge(array('links' => $links), $item);
    }
  }
}
?>

To use this in your theme just put the code in your template.php and exchange "template" in the function name with your theme name. Note that there are also preprocessing functions for modules available.

Help improve this page

Page status: Not set

You can: