1 entityreference.module entityreference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display)

Implements hook_field_formatter_view().

File

core/modules/entityreference/entityreference.module, line 1319
Entityreference primary module file.

Code

function entityreference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $result = array();
  $settings = $display['settings'];
  $field_type_settings = entityreference_field_type_settings($field);
  $target_type = $field_type_settings['entity_type'];
  $column = $field_type_settings['column'];

  switch ($display['type']) {
    case 'entityreference_label':
      $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);

      foreach ($items as $delta => $item) {
        // Skip an item that is not accessible, unless allowing output of
        // entity labels without considering access.
        if (empty($item['access']) && !$display['settings']['bypass_access']) {
          continue;
        }

        // Calling EntityReferenceHandler::getLabel() would make a repeated,
        // wasteful call to entity_access().
        $label = entity_label($field['settings']['target_type'], $item['entity']);

        // Check if the settings and access allow a link to be displayed.
        $display_link = $display['settings']['link'] && $item['access'];

        $uri = NULL;

        // If the link is allowed and the entity has a uri, display a link.
        if ($display_link) {
          $uri = entity_uri($target_type, $item['entity']);
        }

        $result[$delta] = array(
          '#theme' => 'entityreference_label',
          '#label' => $label,
          '#item' => $item,
          '#uri' => $uri,
          '#settings' => array(
            'display' => $display['settings'],
            'field' => $field['settings'],
          ),
        );
      }
      break;

    case 'entityreference_entity_id':
      foreach ($items as $delta => $item) {
        // Skip an item that is not accessible.
        if (empty($item['access'])) {
          continue;
        }

        $result[$delta] = array(
          '#theme' => 'entityreference_entity_id',
          '#item' => $item,
          '#settings' => array(
            'display' => $display['settings'],
            'field' => $field['settings'],
          ),
        );
      }
      break;

    case 'entityreference_entity_view':
      $target_langcode = $langcode;
      if (!empty($settings['use_content_language']) && !empty($GLOBALS['language_content']->language)) {
        $target_langcode = $GLOBALS['language_content']->language;
      }

      foreach ($items as $delta => $item) {
        // Skip an item that is not accessible.
        if (empty($item['access'])) {
          continue;
        }

        // Protect 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' => $target_type, '@entity_id' => $item[$column])));
        }

        $target_entity = clone $item['entity'];
        $target_entity->buildContent($settings['view_mode'], $target_langcode);
        $result[$delta][$target_type][$item[$column]] = $target_entity->view($settings['view_mode'], $target_langcode, !empty($settings['hide_title']));
        if (empty($settings['links']) && isset($result[$delta][$target_type][$item[$column]]['links'])) {
          $result[$delta][$target_type][$item[$column]]['links']['#access'] = FALSE;
        }
        $result[$delta][$target_type][$item[$column]]['#theme'] .= '__entityreference';
        $depth = 0;
      }
      break;
  }

  return $result;
}