You are here

function er_viewmode_field_formatter_view in Entity reference viewmode selector 7

Implements hook_field_formatter_view().

File

./er_viewmode.module, line 120

Code

function er_viewmode_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  if (!er_viewmode_supported_field_widget($field)) {
    return entityreference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
  }
  $result = array();
  $settings = $display['settings'];

  // Rebuild the items list to contain only those with access.
  foreach ($items as $key => $item) {
    if (empty($item['access'])) {
      unset($items[$key]);
    }
  }
  switch ($display['type']) {
    case 'entityreference_entity_view':
      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);
        $result[$delta] = entity_view($field['settings']['target_type'], array(
          $item['target_id'] => $entity,
        ), $item['view_mode'], $langcode, FALSE);
        if (empty($settings['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;
      }
      break;
  }
  return $result;
}