You are here

function multifield_field_prepare_view in Multifield 7

Same name and namespace in other branches
  1. 7.2 multifield.field.inc \multifield_field_prepare_view()

Implements hook_field_prepare_view().

File

./multifield.field.inc, line 857
Field integration for the Multifield module.

Code

function multifield_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $display = NULL) {

  // When this hook is invoked using field_attach_prepare_view(), the view mode
  // being used to render the entities is not passed through here. So we need
  // a hack to retrieve it. When this hook is invoked using field_view_field(),
  // the display settings are passed into the last parameter.
  if (!isset($display)) {
    $backtrace = debug_backtrace();
    foreach ($backtrace as $caller) {
      if ($caller['function'] == 'field_attach_prepare_view') {
        $display = $caller['args'][2];
      }
    }
    if (!isset($display)) {
      throw new Exception("Unable to determine the view mode being used to render the parent entity of the multifield.");
    }
  }
  $machine_name = multifield_extract_multifield_machine_name($field);
  $view_mode_pseudo_entities = array();
  foreach ($entities as $id => $entity) {
    $instance_display = is_array($display) ? $display : field_get_display($instances[$id], $display, $entity);
    if (!$instance_display['type'] !== 'hidden') {
      $view_mode = !empty($display['settings']['view_mode']) ? $display['settings']['view_mode'] : 'default';
      foreach ($items[$id] as $delta => $item) {
        $pseudo_entity = _multifield_field_item_to_entity($machine_name, $item);
        $pseudo_entity->delta = $delta;
        $pseudo_entity->parent_id = $id;
        $view_mode_pseudo_entities[$view_mode][$pseudo_entity->id] = $pseudo_entity;
      }
    }
  }
  foreach ($view_mode_pseudo_entities as $view_mode => $pseudo_entities) {
    field_attach_prepare_view('multifield', $pseudo_entities, $view_mode, $langcode);
    foreach ($pseudo_entities as $pseudo_entity) {
      $item =& $items[$pseudo_entity->parent_id][$pseudo_entity->delta];
      unset($pseudo_entity->delta);
      unset($pseudo_entity->parent_id);
      $item = _multifield_field_entity_to_item($pseudo_entity);
      $item['#pseudo_entity'] = $pseudo_entity;
    }
  }
}