EntityReferenceEntityFormatter.php 3.48 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\entity_reference\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter.
6 7
 */

8
namespace Drupal\entity_reference\Plugin\Field\FieldFormatter;
9

10
use Drupal\Core\Entity\Field\FieldItemListInterface;
11 12 13 14 15
use Drupal\entity_reference\RecursiveRenderingException;

/**
 * Plugin implementation of the 'entity reference rendered entity' formatter.
 *
16
 * @FieldFormatter(
17 18 19 20 21 22 23
 *   id = "entity_reference_entity_view",
 *   label = @Translation("Rendered entity"),
 *   description = @Translation("Display the referenced entities rendered by entity_view()."),
 *   field_types = {
 *     "entity_reference"
 *   },
 *   settings = {
24
 *     "view_mode" = "default",
25 26 27 28 29 30 31
 *     "link" = FALSE
 *   }
 * )
 */
class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase {

  /**
32
   * {@inheritdoc}
33 34
   */
  public function settingsForm(array $form, array &$form_state) {
35
    $view_modes = entity_get_view_modes($this->getFieldSetting('target_type'));
36
    $options = array('default' => t('Default'));
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    foreach ($view_modes as $view_mode => $view_mode_settings) {
      $options[$view_mode] = $view_mode_settings['label'];
    }

    $elements['view_mode'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#title' => t('View mode'),
      '#default_value' => $this->getSetting('view_mode'),
      '#required' => TRUE,
    );

    $elements['links'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show links'),
      '#default_value' => $this->getSetting('links'),
    );

    return $elements;
  }

  /**
59
   * {@inheritdoc}
60 61 62 63
   */
  public function settingsSummary() {
    $summary = array();

64
    $view_modes = entity_get_view_modes($this->getFieldSetting('target_type'));
65
    $view_mode = $this->getSetting('view_mode');
66 67 68
    if ($view_mode == 'default') {
      $view_mode = t('Default');
    }
69 70 71
    $summary[] = t('Rendered as @mode', array('@mode' => isset($view_modes[$view_mode]['label']) ? $view_modes[$view_mode]['label'] : $view_mode));
    $summary[] = $this->getSetting('links') ? t('Display links') : t('Do not display links');

72
    return $summary;
73 74 75
  }

  /**
76
   * {@inheritdoc}
77
   */
78
  public function viewElements(FieldItemListInterface $items) {
79 80 81
    $view_mode = $this->getSetting('view_mode');
    $links = $this->getSetting('links');

82
    $target_type = $this->getFieldSetting('target_type');
83 84 85 86

    $elements = array();

    foreach ($items as $delta => $item) {
87 88 89 90
      if (!$item->access) {
        // User doesn't have access to the referenced entity.
        continue;
      }
91 92 93 94
      // Protect ourselves from recursive rendering.
      static $depth = 0;
      $depth++;
      if ($depth > 20) {
95
        throw new RecursiveRenderingException(format_string('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array('@entity_type' => $item->entity->entityType(), '@entity_id' => $item->target_id)));
96 97
      }

98 99
      if (!empty($item->target_id)) {
        $entity = clone $item->entity;
100
        unset($entity->content);
101
        $elements[$delta] = entity_view($entity, $view_mode, $item->getLangcode());
102

103
        if (empty($links) && isset($result[$delta][$target_type][$item->target_id]['links'])) {
104
          // Hide the element links.
105
          $elements[$delta][$target_type][$item->target_id]['links']['#access'] = FALSE;
106 107 108 109
        }
      }
      else {
        // This is an "auto_create" item.
110
        $elements[$delta] = array('#markup' => $item->entity->label());
111 112 113 114 115 116
      }
      $depth = 0;
    }

    return $elements;
  }
117

118
}