Created
January 12, 2015 20:34
-
-
Save heyMP/7bd11fe4e729a842cb30 to your computer and use it in GitHub Desktop.
How to theme field-collections in Drupal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php foreach($rows as $row): ?> | |
| <div class="vp-update__update"> | |
| <div class="vp-update__updatestitle"> | |
| <?php print render($row['field_vpupdates_updatetitle']); ?> | |
| </div> | |
| <div class="vp-update__updatesbody"> | |
| <?php print render($row['field_vpupdates_updatebody']); ?> | |
| </div> | |
| </div> | |
| <?php endforeach; ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Implements hook_preprocess_field(). | |
| */ | |
| function itcomm_omega_sub_preprocess_field(&$vars) { | |
| // For all Field collections | |
| if ($vars['element']['#field_name'] == 'field_vpupdates_updates') { | |
| $vars['theme_hook_suggestions'][] = 'field__vpupdates_updates'; | |
| $field_array = array('field_vpupdates_updatetitle', 'field_vpupdates_updatebody'); | |
| rows_from_field_collection(&$vars, 'field_vpupdates_updates', $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; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a variation of the following Four Kitchens solution: http://fourword.fourkitchens.com/article/better-way-theme-field-collections.
I prefer to load the render array of each field rather than the raw value.