You are here

function entityreference_multiple_field_formatter_view in Entity reference multiple display 7

Implements hook_field_formatter_view().

File

./entityreference_multiple.module, line 224
Extend entity reference formatter with an option to separate elements display with specific view_modes.

Code

function entityreference_multiple_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $result = $split = $test = array();

  // Initialize the var
  if ($display['type'] == 'entityreference_entity_multiple_view') {

    // we define number of elements by group
    $count = 0;
    for ($i = 1; $i <= $settings['view_mode_number']; $i++) {

      // We need to get number of last elements
      $count += $settings['view_mode_multiple']['view_mode_' . $i . '_number'];
      if ($settings['view_mode_multiple']['view_mode_' . $i . '_number'] == '#') {
        $settings['view_mode_multiple']['view_mode_' . $i . '_number'] = count($items) - $count;
      }

      // We create a reference to get the view mode for each element
      for ($j = 1; $j <= $settings['view_mode_multiple']['view_mode_' . $i . '_number']; $j++) {
        $split[] = array(
          'view_mode' => $settings['view_mode_multiple']['view_mode_' . $i],
          'links' => isset($settings['view_mode_multiple']['view_mode_' . $i . '_links']) ? $settings['view_mode_multiple']['view_mode_' . $i . '_links'] : '',
        );
      }
    }
    foreach ($items as $delta => $item) {

      // Protect ourselves from recursive rendering.
      static $depth = 0;
      $depth++;
      if ($depth > 20) {
        throw new EntityReferenceRecursiveRenderingException(t('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array(
          '@entity_type' => $entity_type,
          '@entity_id' => $item['target_id'],
        )));
      }
      $entity = clone $item['entity'];
      unset($entity->content);

      // we render each element with the good view mode
      $result[$delta] = entity_view($field['settings']['target_type'], array(
        $item['target_id'] => $entity,
      ), $split[$delta]['view_mode'], $langcode, FALSE);
      if (empty($split[$delta]['links']) && isset($result[$delta][$field['settings']['target_type']][$item['target_id']]['links'])) {
        $result[$delta][$field['settings']['target_type']][$item['target_id']]['links']['#access'] = FALSE;
      }
      $depth = 0;
    }
  }
  return $result;
}