Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zerolab/2bddaa24bea5c221c39f to your computer and use it in GitHub Desktop.
Save zerolab/2bddaa24bea5c221c39f to your computer and use it in GitHub Desktop.
<?php
/**
* Implements hook_entity_view_alter()
*
* Task: change child field_collection view mode depending on parent
* field_block_layout value
*
* Solution: Screw that. Instead change the image style for
* field_collection_imagecaption in the field_collection_content_block
* when the layout is two columns.
*
* Notes: ds_get_field_settings() does not work. The child field_collection
* fields view_modes need to be changed to the new view mode settings
* in order for the whole thing to work. In our case, the image style is most
* important, so... will have to hard-coded it. :/
*/
function theming_fixes_entity_view_alter(&$build, $type) {
if ($build['#bundle'] != 'field_collection_content_block') {
return;
}
$entity = &$build['#entity'];
$layout = $entity->field_block_layout[LANGUAGE_NONE][0]['value'];
if (!in_array($layout, array('half-width-left', 'half-width-right'))) {
return;
}
foreach ($build as $key => &$field) {
if (strpos($key, 'field_collection') === FALSE) {
continue;
}
foreach ($field['#items'] as $index => $collection_item) {
$id = $collection_item['value'];
$item = &$field[$index]['entity']['field_collection_item'][$id];
if ($item['#bundle'] == 'field_collection_imagecaption') {
$image = &$item['field_image_main'];
$image['#view_mode'] = 'two_column_content';
foreach(array_keys($image['#items']) as $image_index) {
$image[$image_index]['#image_style'] = 'half_page_width_345px';
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment