Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity_reference_revisions / src / Plugin / Field / FieldFormatter / EntityReferenceRevisionsEntityFormatter.php
1 <?php
2
3 namespace Drupal\entity_reference_revisions\Plugin\Field\FieldFormatter;
4
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;
12
13 /**
14  * Plugin implementation of the 'entity reference rendered entity' formatter.
15  *
16  * @FieldFormatter(
17  *   id = "entity_reference_revisions_entity_view",
18  *   label = @Translation("Rendered entity"),
19  *   description = @Translation("Display the referenced entities rendered by entity_view()."),
20  *   field_types = {
21  *     "entity_reference_revisions"
22  *   }
23  * )
24  */
25 class EntityReferenceRevisionsEntityFormatter extends EntityReferenceRevisionsFormatterBase implements ContainerFactoryPluginInterface {
26
27   /**
28    * The logger factory.
29    *
30    * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
31    */
32   protected $loggerFactory;
33
34   /**
35    * The entity display repository.
36    *
37    * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
38    */
39   protected $entityDisplayRepository;
40
41   /**
42    * Constructs a StringFormatter instance.
43    *
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
55    *   The view mode.
56    * @param array $third_party_settings
57    *   Any third party settings settings.
58    * @param LoggerChannelFactoryInterface $logger_factory
59    *   The logger factory.
60    * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
61    *   The entity display repository.
62    */
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;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
73     return new static(
74       $plugin_id,
75       $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')
83     );
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public static function defaultSettings() {
90     return array(
91       'view_mode' => 'default',
92       'link' => FALSE,
93     ) + parent::defaultSettings();
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function settingsForm(array $form, FormStateInterface $form_state) {
100     $elements['view_mode'] = array(
101       '#type' => 'select',
102       '#options' => $this->entityDisplayRepository->getViewModeOptions($this->getFieldSetting('target_type')),
103       '#title' => $this->t('View mode'),
104       '#default_value' => $this->getSetting('view_mode'),
105       '#required' => TRUE,
106     );
107
108     return $elements;
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function settingsSummary() {
115     $summary = array();
116
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));
120
121     return $summary;
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function viewElements(FieldItemListInterface $items, $langcode) {
128     $view_mode = $this->getSetting('view_mode');
129     $elements = array();
130
131     foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
132       // Protect ourselves from recursive rendering.
133       static $depth = 0;
134       $depth++;
135       if ($depth > 20) {
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()));
137         return $elements;
138       }
139       $view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
140       $elements[$delta] = $view_builder->view($entity, $view_mode, $entity->language()->getId());
141
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());
147       }
148       $depth = 0;
149     }
150
151     return $elements;
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public static function isApplicable(FieldDefinitionInterface $field_definition) {
158     // This formatter is only available for entity types that have a view
159     // builder.
160     $target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
161     return \Drupal::entityTypeManager()->getDefinition($target_type)->hasViewBuilderClass();
162   }
163
164 }