You are here

class EntityReferenceEntityFormatter in Entity reference 8

Plugin implementation of the 'entity-reference rendered entity' formatter.

Plugin annotation


@Plugin(
  id = "entityreference_entity_view",
  module = "entityreference",
  label = @Translation("Rendered entity"),
  description = @Translation("Display the referenced entities rendered by entity_view()."),
  field_types = {
    "entityreference"
  },
  settings = {
    "view_mode" = "",
    "link" = "FALSE"
  }
)

Hierarchy

Expanded class hierarchy of EntityReferenceEntityFormatter

File

lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceEntityFormatter.php, line 34
Definition of Drupal\entityreference\Plugin\field\formatter\EntityReferenceEntityFormatter.

Namespace

Drupal\entityreference\Plugin\field\formatter
View source
class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase {

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
   */
  public function settingsForm(array $form, array &$form_state) {
    $entity_info = entity_get_info($this->field['settings']['target_type']);
    $options = array();
    if (!empty($entity_info['view modes'])) {
      foreach ($entity_info['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;
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
   */
  public function settingsSummary() {
    $summary = array();
    $entity_info = entity_get_info($this->field['settings']['target_type']);
    $view_mode = $this
      ->getSetting('view_mode');
    $summary[] = t('Rendered as @mode', array(
      '@mode' => isset($entity_info['view modes'][$view_mode]['label']) ? $entity_info['view modes'][$view_mode]['label'] : $view_mode,
    ));
    $summary[] = $this
      ->getSetting('links') ? t('Display links') : t('Do not display links');
    return implode('<br />', $summary);
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
   */
  public function viewElements(EntityInterface $entity, $langcode, array $items) {

    // Remove un-accessible items.
    parent::viewElements($entity, $langcode, $items);
    $instance = $this->instance;
    $field = $this->field;
    $view_mode = $this
      ->getSetting('view_mode');
    $links = $this
      ->getSetting('links');
    $target_type = $field['settings']['target_type'];
    $elements = array();
    foreach ($items as $delta => $item) {

      // Protect ourselves from recursive rendering.
      static $depth = 0;
      $depth++;
      if ($depth > 20) {
        throw new EntityReferenceRecursiveRenderingException(format_string('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);
      $elements[$delta] = entity_view($entity, $view_mode, $langcode);
      if (empty($links) && isset($result[$delta][$target_type][$item['target_id']]['links'])) {

        // Hide the element links.
        $elements[$delta][$target_type][$item['target_id']]['links']['#access'] = FALSE;
      }
      $depth = 0;
    }
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceEntityFormatter::settingsForm public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
EntityReferenceEntityFormatter::settingsSummary public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
EntityReferenceEntityFormatter::viewElements public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). Overrides EntityReferenceFormatterBase::viewElements
EntityReferenceFormatterBase::prepareView public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView().