Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Plugin / views / row / EntityRow.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\row;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Language\LanguageManagerInterface;
8 use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
9 use Drupal\views\Plugin\views\display\DisplayPluginBase;
10 use Drupal\views\ViewExecutable;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Generic entity row plugin to provide a common base for all entity types.
15  *
16  * @ViewsRow(
17  *   id = "entity",
18  *   deriver = "Drupal\views\Plugin\Derivative\ViewsEntityRow"
19  * )
20  */
21 class EntityRow extends RowPluginBase {
22   use EntityTranslationRenderTrait;
23
24   /**
25    * The table the entity is using for storage.
26    *
27    * @var string
28    */
29   public $base_table;
30
31   /**
32    * The actual field which is used for the entity id.
33    *
34    * @var string
35    */
36   public $base_field;
37
38   /**
39    * Stores the entity type ID of the result entities.
40    *
41    * @var string
42    */
43   protected $entityTypeId;
44
45   /**
46    * Contains the entity type of this row plugin instance.
47    *
48    * @var \Drupal\Core\Entity\EntityTypeInterface
49    */
50   protected $entityType;
51
52   /**
53    * The entity manager.
54    *
55    * @var \Drupal\Core\Entity\EntityManagerInterface
56    */
57   public $entityManager;
58
59   /**
60    * The language manager.
61    *
62    * @var \Drupal\Core\Language\LanguageManagerInterface
63    */
64   protected $languageManager;
65
66   /**
67    * {@inheritdoc}
68    *
69    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
70    *   The entity manager.
71    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
72    *   The language manager.
73    */
74   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
75     parent::__construct($configuration, $plugin_id, $plugin_definition);
76
77     $this->entityManager = $entity_manager;
78     $this->languageManager = $language_manager;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
85     parent::init($view, $display, $options);
86
87     $this->entityTypeId = $this->definition['entity_type'];
88     $this->entityType = $this->entityManager->getDefinition($this->entityTypeId);
89     $this->base_table = $this->entityType->getDataTable() ?: $this->entityType->getBaseTable();
90     $this->base_field = $this->entityType->getKey('id');
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
97     return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity.manager'), $container->get('language_manager'));
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function getEntityTypeId() {
104     return $this->entityType->id();
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   protected function getEntityManager() {
111     return $this->entityManager;
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   protected function getLanguageManager() {
118     return $this->languageManager;
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   protected function getView() {
125     return $this->view;
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   protected function defineOptions() {
132     $options = parent::defineOptions();
133     $options['view_mode'] = ['default' => 'default'];
134     return $options;
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
141     parent::buildOptionsForm($form, $form_state);
142
143     $form['view_mode'] = [
144       '#type' => 'select',
145       '#options' => \Drupal::entityManager()->getViewModeOptions($this->entityTypeId),
146       '#title' => $this->t('View mode'),
147       '#default_value' => $this->options['view_mode'],
148     ];
149   }
150
151   /**
152    * {@inheritdoc}
153    */
154   public function summaryTitle() {
155     $options = \Drupal::entityManager()->getViewModeOptions($this->entityTypeId);
156     if (isset($options[$this->options['view_mode']])) {
157       return $options[$this->options['view_mode']];
158     }
159     else {
160       return $this->t('No view mode selected');
161     }
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public function query() {
168     parent::query();
169     $this->getEntityTranslationRenderer()->query($this->view->getQuery());
170   }
171
172   /**
173    * {@inheritdoc}
174    */
175   public function preRender($result) {
176     parent::preRender($result);
177     if ($result) {
178       $this->getEntityTranslationRenderer()->preRender($result);
179     }
180   }
181
182   /**
183    * {@inheritdoc}
184    */
185   public function render($row) {
186     return $this->getEntityTranslationRenderer()->render($row);
187   }
188
189   /**
190    * {@inheritdoc}
191    */
192   public function calculateDependencies() {
193     $dependencies = parent::calculateDependencies();
194
195     $view_mode = $this->entityManager
196       ->getStorage('entity_view_mode')
197       ->load($this->entityTypeId . '.' . $this->options['view_mode']);
198     if ($view_mode) {
199       $dependencies[$view_mode->getConfigDependencyKey()][] = $view_mode->getConfigDependencyName();
200     }
201
202     return $dependencies;
203   }
204
205 }