Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Josh-Miller/d11894805dc81731ec8b to your computer and use it in GitHub Desktop.

Select an option

Save Josh-Miller/d11894805dc81731ec8b to your computer and use it in GitHub Desktop.
<?php if (isset($rows)): ?>
<?php foreach($rows as $row): ?>
<div class="field-row">
<div class="field-item">
<?php print render($row['field_some_field']); ?>
</div>
<div class="field-item">
<?php print render($row['field_someother_field']); ?>
</div>
</div>
<?php endforeach; ?>
<?php
/**
* Implements hook_preprocess_field().
*/
function THEME_preprocess_field(&$vars) {
// For all Field collections
if ($vars['element']['#field_name'] == 'field_my_fieldcollection') {
$vars['theme_hook_suggestions'][] = 'field__field_my_fieldcollection';
$field_array = array('field_some_field', 'field_someother_field');
rows_from_field_collection($vars, 'field_my_fieldcollection', $field_array);
}
}
/**
* Creates a simple text rows array from a field collections, to be used in a
* field_preprocess function.
*
* @param $vars
* An array of variables to pass to the theme template.
*
* @param $field_name
* The name of the field being altered.
*
* @param $field_array
* Array of fields to be turned into rows in the field collection.
*/
function rows_from_field_collection(&$vars, $field_name, $field_array) {
$vars['rows'] = array();
foreach($vars['element']['#items'] as $key => $item) {
$entity_id = $item['value'];
$entity = field_collection_item_load($entity_id);
$wrapper = entity_metadata_wrapper('field_collection_item', $entity);
$row = array();
foreach($field_array as $field){
$row[$field] = field_view_field('field_collection_item', $entity, $field, 'full');
}
$vars['rows'][] = $row;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment