View source
<?php
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase {
const RECURSIVE_RENDER_LIMIT = 20;
protected $loggerFactory;
protected $entityTypeManager;
protected $entityDisplayRepository;
protected static $recursiveRenderDepth = [];
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, LoggerChannelFactoryInterface $logger_factory, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->loggerFactory = $logger_factory;
$this->entityTypeManager = $entity_type_manager;
$this->entityDisplayRepository = $entity_display_repository;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('logger.factory'), $container
->get('entity_type.manager'), $container
->get('entity_display.repository'));
}
public static function defaultSettings() {
return [
'view_mode' => 'default',
'link' => FALSE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['view_mode'] = [
'#type' => 'select',
'#options' => $this->entityDisplayRepository
->getViewModeOptions($this
->getFieldSetting('target_type')),
'#title' => t('View mode'),
'#default_value' => $this
->getSetting('view_mode'),
'#required' => TRUE,
];
return $elements;
}
public function settingsSummary() {
$summary = [];
$view_modes = $this->entityDisplayRepository
->getViewModeOptions($this
->getFieldSetting('target_type'));
$view_mode = $this
->getSetting('view_mode');
$summary[] = t('Rendered as @mode', [
'@mode' => isset($view_modes[$view_mode]) ? $view_modes[$view_mode] : $view_mode,
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$view_mode = $this
->getSetting('view_mode');
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
$recursive_render_id = $items
->getFieldDefinition()
->getTargetEntityTypeId() . $items
->getFieldDefinition()
->getTargetBundle() . $items
->getName() . $items
->getEntity()
->id() . $entity
->getEntityTypeId() . $entity
->id();
if (isset(static::$recursiveRenderDepth[$recursive_render_id])) {
static::$recursiveRenderDepth[$recursive_render_id]++;
}
else {
static::$recursiveRenderDepth[$recursive_render_id] = 1;
}
if (static::$recursiveRenderDepth[$recursive_render_id] > static::RECURSIVE_RENDER_LIMIT) {
$this->loggerFactory
->get('entity')
->error('Recursive rendering detected when rendering entity %entity_type: %entity_id, using the %field_name field on the %parent_entity_type:%parent_bundle %parent_entity_id entity. Aborting rendering.', [
'%entity_type' => $entity
->getEntityTypeId(),
'%entity_id' => $entity
->id(),
'%field_name' => $items
->getName(),
'%parent_entity_type' => $items
->getFieldDefinition()
->getTargetEntityTypeId(),
'%parent_bundle' => $items
->getFieldDefinition()
->getTargetBundle(),
'%parent_entity_id' => $items
->getEntity()
->id(),
]);
return $elements;
}
$view_builder = $this->entityTypeManager
->getViewBuilder($entity
->getEntityTypeId());
$elements[$delta] = $view_builder
->view($entity, $view_mode, $entity
->language()
->getId());
if (!empty($items[$delta]->_attributes) && !$entity
->isNew() && $entity
->hasLinkTemplate('canonical')) {
$items[$delta]->_attributes += [
'resource' => $entity
->toUrl()
->toString(),
];
}
}
return $elements;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$target_type = $field_definition
->getFieldStorageDefinition()
->getSetting('target_type');
return \Drupal::entityTypeManager()
->getDefinition($target_type)
->hasViewBuilderClass();
}
}