3 namespace Drupal\entity_reference_revisions\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Plugin implementation of the 'entity reference rendered entity' formatter.
17 * id = "entity_reference_revisions_entity_view",
18 * label = @Translation("Rendered entity"),
19 * description = @Translation("Display the referenced entities rendered by entity_view()."),
21 * "entity_reference_revisions"
25 class EntityReferenceRevisionsEntityFormatter extends EntityReferenceRevisionsFormatterBase implements ContainerFactoryPluginInterface {
30 * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
32 protected $loggerFactory;
35 * The entity display repository.
37 * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
39 protected $entityDisplayRepository;
42 * Constructs a StringFormatter instance.
44 * @param string $plugin_id
45 * The plugin_id for the formatter.
46 * @param mixed $plugin_definition
47 * The plugin implementation definition.
48 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
49 * The definition of the field to which the formatter is associated.
50 * @param array $settings
51 * The formatter settings.
52 * @param string $label
53 * The formatter label display setting.
54 * @param string $view_mode
56 * @param array $third_party_settings
57 * Any third party settings settings.
58 * @param LoggerChannelFactoryInterface $logger_factory
60 * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
61 * The entity display repository.
63 public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, LoggerChannelFactoryInterface $logger_factory, EntityDisplayRepositoryInterface $entity_display_repository) {
64 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
65 $this->loggerFactory = $logger_factory;
66 $this->entityDisplayRepository = $entity_display_repository;
72 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
76 $configuration['field_definition'],
77 $configuration['settings'],
78 $configuration['label'],
79 $configuration['view_mode'],
80 $configuration['third_party_settings'],
81 $container->get('logger.factory'),
82 $container->get('entity_display.repository')
89 public static function defaultSettings() {
91 'view_mode' => 'default',
93 ) + parent::defaultSettings();
99 public function settingsForm(array $form, FormStateInterface $form_state) {
100 $elements['view_mode'] = array(
102 '#options' => $this->entityDisplayRepository->getViewModeOptions($this->getFieldSetting('target_type')),
103 '#title' => $this->t('View mode'),
104 '#default_value' => $this->getSetting('view_mode'),
114 public function settingsSummary() {
117 $view_modes = $this->entityDisplayRepository->getViewModeOptions($this->getFieldSetting('target_type'));
118 $view_mode = $this->getSetting('view_mode');
119 $summary[] = $this->t('Rendered as @mode', array('@mode' => isset($view_modes[$view_mode]) ? $view_modes[$view_mode] : $view_mode));
127 public function viewElements(FieldItemListInterface $items, $langcode) {
128 $view_mode = $this->getSetting('view_mode');
131 foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
132 // Protect ourselves from recursive rendering.
136 $this->loggerFactory->get('entity')->error('Recursive rendering detected when rendering entity @entity_type @entity_id. Aborting rendering.', array('@entity_type' => $entity->getEntityTypeId(), '@entity_id' => $entity->id()));
139 $view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
140 $elements[$delta] = $view_builder->view($entity, $view_mode, $entity->language()->getId());
142 // Add a resource attribute to set the mapping property's value to the
143 // entity's url. Since we don't know what the markup of the entity will
144 // be, we shouldn't rely on it for structured data such as RDFa.
145 if (!empty($items[$delta]->_attributes) && !$entity->isNew() && $entity->hasLinkTemplate('canonical')) {
146 $items[$delta]->_attributes += array('resource' => $entity->toUrl()->toString());
157 public static function isApplicable(FieldDefinitionInterface $field_definition) {
158 // This formatter is only available for entity types that have a view
160 $target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
161 return \Drupal::entityTypeManager()->getDefinition($target_type)->hasViewBuilderClass();