Community Documentation

Example: Altering the structure of field collection items

Last updated July 30, 2012. Created by archnode on January 23, 2012.
Edited by kalman.hosszu. Log in to edit this page.

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.

About this page

Drupal version
Drupal 7.x
Level
Beginner
Audience
Designers/themers

Develop for Drupal

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here