Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\views\EventSubscriber;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityTypeEventSubscriberTrait;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityTypeListenerInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
|
Chris@0
|
10 use Drupal\views\Views;
|
Chris@0
|
11 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Reacts to changes on entity types to update all views entities.
|
Chris@0
|
15 */
|
Chris@0
|
16 class ViewsEntitySchemaSubscriber implements EntityTypeListenerInterface, EventSubscriberInterface {
|
Chris@0
|
17
|
Chris@0
|
18 use EntityTypeEventSubscriberTrait;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Indicates that a base table got renamed.
|
Chris@0
|
22 */
|
Chris@0
|
23 const BASE_TABLE_RENAME = 0;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Indicates that a data table got renamed.
|
Chris@0
|
27 */
|
Chris@0
|
28 const DATA_TABLE_RENAME = 1;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Indicates that a data table got added.
|
Chris@0
|
32 */
|
Chris@0
|
33 const DATA_TABLE_ADDITION = 2;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Indicates that a data table got removed.
|
Chris@0
|
37 */
|
Chris@0
|
38 const DATA_TABLE_REMOVAL = 3;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Indicates that a revision table got renamed.
|
Chris@0
|
42 */
|
Chris@0
|
43 const REVISION_TABLE_RENAME = 4;
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Indicates that a revision table got added.
|
Chris@0
|
47 */
|
Chris@0
|
48 const REVISION_TABLE_ADDITION = 5;
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Indicates that a revision table got removed.
|
Chris@0
|
52 */
|
Chris@0
|
53 const REVISION_TABLE_REMOVAL = 6;
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Indicates that a revision data table got renamed.
|
Chris@0
|
57 */
|
Chris@0
|
58 const REVISION_DATA_TABLE_RENAME = 7;
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * Indicates that a revision data table got added.
|
Chris@0
|
62 */
|
Chris@0
|
63 const REVISION_DATA_TABLE_ADDITION = 8;
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Indicates that a revision data table got removed.
|
Chris@0
|
67 */
|
Chris@0
|
68 const REVISION_DATA_TABLE_REMOVAL = 9;
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * The entity manager.
|
Chris@0
|
72 *
|
Chris@0
|
73 * @var \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
74 */
|
Chris@0
|
75 protected $entityManager;
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Constructs a ViewsEntitySchemaSubscriber.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
Chris@0
|
81 * The entity manager.
|
Chris@0
|
82 */
|
Chris@0
|
83 public function __construct(EntityManagerInterface $entity_manager) {
|
Chris@0
|
84 $this->entityManager = $entity_manager;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * {@inheritdoc}
|
Chris@0
|
89 */
|
Chris@0
|
90 public static function getSubscribedEvents() {
|
Chris@0
|
91 return static::getEntityTypeEvents();
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * {@inheritdoc}
|
Chris@0
|
96 */
|
Chris@0
|
97 public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
|
Chris@0
|
98 $changes = [];
|
Chris@0
|
99
|
Chris@0
|
100 // We implement a specific logic for table updates, which is bound to the
|
Chris@0
|
101 // default sql content entity storage.
|
Chris@0
|
102 if (!$this->entityManager->getStorage($entity_type->id()) instanceof SqlContentEntityStorage) {
|
Chris@0
|
103 return;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 if ($entity_type->getBaseTable() != $original->getBaseTable()) {
|
Chris@0
|
107 $changes[] = static::BASE_TABLE_RENAME;
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 $revision_add = $entity_type->isRevisionable() && !$original->isRevisionable();
|
Chris@0
|
111 $revision_remove = !$entity_type->isRevisionable() && $original->isRevisionable();
|
Chris@0
|
112 $translation_add = $entity_type->isTranslatable() && !$original->isTranslatable();
|
Chris@0
|
113 $translation_remove = !$entity_type->isTranslatable() && $original->isTranslatable();
|
Chris@0
|
114
|
Chris@0
|
115 if ($revision_add) {
|
Chris@0
|
116 $changes[] = static::REVISION_TABLE_ADDITION;
|
Chris@0
|
117 }
|
Chris@0
|
118 elseif ($revision_remove) {
|
Chris@0
|
119 $changes[] = static::REVISION_TABLE_REMOVAL;
|
Chris@0
|
120 }
|
Chris@0
|
121 elseif ($entity_type->isRevisionable() && $entity_type->getRevisionTable() != $original->getRevisionTable()) {
|
Chris@0
|
122 $changes[] = static::REVISION_TABLE_RENAME;
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 if ($translation_add) {
|
Chris@0
|
126 $changes[] = static::DATA_TABLE_ADDITION;
|
Chris@0
|
127 }
|
Chris@0
|
128 elseif ($translation_remove) {
|
Chris@0
|
129 $changes[] = static::DATA_TABLE_REMOVAL;
|
Chris@0
|
130 }
|
Chris@0
|
131 elseif ($entity_type->isTranslatable() && $entity_type->getDataTable() != $original->getDataTable()) {
|
Chris@0
|
132 $changes[] = static::DATA_TABLE_RENAME;
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 if ($entity_type->isRevisionable() && $entity_type->isTranslatable()) {
|
Chris@0
|
136 if ($revision_add || $translation_add) {
|
Chris@0
|
137 $changes[] = static::REVISION_DATA_TABLE_ADDITION;
|
Chris@0
|
138 }
|
Chris@0
|
139 elseif ($entity_type->getRevisionDataTable() != $original->getRevisionDataTable()) {
|
Chris@0
|
140 $changes[] = static::REVISION_DATA_TABLE_RENAME;
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|
Chris@0
|
143 elseif ($original->isRevisionable() && $original->isTranslatable() && ($revision_remove || $translation_remove)) {
|
Chris@0
|
144 $changes[] = static::REVISION_DATA_TABLE_REMOVAL;
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 // Stop here if no changes are needed.
|
Chris@0
|
148 if (empty($changes)) {
|
Chris@0
|
149 return;
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 /** @var \Drupal\views\Entity\View[] $all_views */
|
Chris@0
|
153 $all_views = $this->entityManager->getStorage('view')->loadMultiple(NULL);
|
Chris@0
|
154
|
Chris@0
|
155 foreach ($changes as $change) {
|
Chris@0
|
156 switch ($change) {
|
Chris@0
|
157 case static::BASE_TABLE_RENAME:
|
Chris@0
|
158 $this->baseTableRename($all_views, $entity_type->id(), $original->getBaseTable(), $entity_type->getBaseTable());
|
Chris@0
|
159 break;
|
Chris@0
|
160 case static::DATA_TABLE_RENAME:
|
Chris@0
|
161 $this->dataTableRename($all_views, $entity_type->id(), $original->getDataTable(), $entity_type->getDataTable());
|
Chris@0
|
162 break;
|
Chris@0
|
163 case static::DATA_TABLE_ADDITION:
|
Chris@0
|
164 $this->dataTableAddition($all_views, $entity_type, $entity_type->getDataTable(), $entity_type->getBaseTable());
|
Chris@0
|
165 break;
|
Chris@0
|
166 case static::DATA_TABLE_REMOVAL:
|
Chris@0
|
167 $this->dataTableRemoval($all_views, $entity_type->id(), $original->getDataTable(), $entity_type->getBaseTable());
|
Chris@0
|
168 break;
|
Chris@0
|
169 case static::REVISION_TABLE_RENAME:
|
Chris@0
|
170 $this->baseTableRename($all_views, $entity_type->id(), $original->getRevisionTable(), $entity_type->getRevisionTable());
|
Chris@0
|
171 break;
|
Chris@0
|
172 case static::REVISION_TABLE_ADDITION:
|
Chris@0
|
173 // If we add revision support we don't have to do anything.
|
Chris@0
|
174 break;
|
Chris@0
|
175 case static::REVISION_TABLE_REMOVAL:
|
Chris@0
|
176 $this->revisionRemoval($all_views, $original);
|
Chris@0
|
177 break;
|
Chris@0
|
178 case static::REVISION_DATA_TABLE_RENAME:
|
Chris@0
|
179 $this->dataTableRename($all_views, $entity_type->id(), $original->getRevisionDataTable(), $entity_type->getRevisionDataTable());
|
Chris@0
|
180 break;
|
Chris@0
|
181 case static::REVISION_DATA_TABLE_ADDITION:
|
Chris@0
|
182 $this->dataTableAddition($all_views, $entity_type, $entity_type->getRevisionDataTable(), $entity_type->getRevisionTable());
|
Chris@0
|
183 break;
|
Chris@0
|
184 case static::REVISION_DATA_TABLE_REMOVAL:
|
Chris@0
|
185 $this->dataTableRemoval($all_views, $entity_type->id(), $original->getRevisionDataTable(), $entity_type->getRevisionTable());
|
Chris@0
|
186 break;
|
Chris@0
|
187 }
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 foreach ($all_views as $view) {
|
Chris@0
|
191 // All changes done to the views here can be trusted and this might be
|
Chris@0
|
192 // called during updates, when it is not safe to rely on configuration
|
Chris@0
|
193 // containing valid schema. Trust the data and disable schema validation
|
Chris@0
|
194 // and casting.
|
Chris@0
|
195 $view->trustData()->save();
|
Chris@0
|
196 }
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 /**
|
Chris@0
|
200 * {@inheritdoc}
|
Chris@0
|
201 */
|
Chris@0
|
202 public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
|
Chris@0
|
203 $tables = [
|
Chris@0
|
204 $entity_type->getBaseTable(),
|
Chris@0
|
205 $entity_type->getDataTable(),
|
Chris@0
|
206 $entity_type->getRevisionTable(),
|
Chris@0
|
207 $entity_type->getRevisionDataTable(),
|
Chris@0
|
208 ];
|
Chris@0
|
209
|
Chris@0
|
210 $all_views = $this->entityManager->getStorage('view')->loadMultiple(NULL);
|
Chris@0
|
211 /** @var \Drupal\views\Entity\View $view */
|
Chris@0
|
212 foreach ($all_views as $id => $view) {
|
Chris@0
|
213
|
Chris@0
|
214 // First check just the base table.
|
Chris@0
|
215 if (in_array($view->get('base_table'), $tables)) {
|
Chris@0
|
216 $view->disable();
|
Chris@0
|
217 $view->save();
|
Chris@0
|
218 }
|
Chris@0
|
219 }
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 /**
|
Chris@0
|
223 * Applies a callable onto all handlers of all passed in views.
|
Chris@0
|
224 *
|
Chris@0
|
225 * @param \Drupal\views\Entity\View[] $all_views
|
Chris@0
|
226 * All views entities.
|
Chris@0
|
227 * @param callable $process
|
Chris@0
|
228 * A callable which retrieves a handler config array.
|
Chris@0
|
229 */
|
Chris@0
|
230 protected function processHandlers(array $all_views, callable $process) {
|
Chris@0
|
231 foreach ($all_views as $view) {
|
Chris@0
|
232 foreach (array_keys($view->get('display')) as $display_id) {
|
Chris@0
|
233 $display = &$view->getDisplay($display_id);
|
Chris@0
|
234 foreach (Views::getHandlerTypes() as $handler_type) {
|
Chris@0
|
235 $handler_type = $handler_type['plural'];
|
Chris@0
|
236 if (!isset($display['display_options'][$handler_type])) {
|
Chris@0
|
237 continue;
|
Chris@0
|
238 }
|
Chris@0
|
239 foreach ($display['display_options'][$handler_type] as $id => &$handler_config) {
|
Chris@0
|
240 $process($handler_config);
|
Chris@0
|
241 if ($handler_config === NULL) {
|
Chris@0
|
242 unset($display['display_options'][$handler_type][$id]);
|
Chris@0
|
243 }
|
Chris@0
|
244 }
|
Chris@0
|
245 }
|
Chris@0
|
246 }
|
Chris@0
|
247 }
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@0
|
250 /**
|
Chris@0
|
251 * Updates views if a base table is renamed.
|
Chris@0
|
252 *
|
Chris@0
|
253 * @param \Drupal\views\Entity\View[] $all_views
|
Chris@0
|
254 * All views.
|
Chris@0
|
255 * @param string $entity_type_id
|
Chris@0
|
256 * The entity type ID.
|
Chris@0
|
257 * @param string $old_base_table
|
Chris@0
|
258 * The old base table name.
|
Chris@0
|
259 * @param string $new_base_table
|
Chris@0
|
260 * The new base table name.
|
Chris@0
|
261 */
|
Chris@0
|
262 protected function baseTableRename($all_views, $entity_type_id, $old_base_table, $new_base_table) {
|
Chris@0
|
263 foreach ($all_views as $view) {
|
Chris@0
|
264 if ($view->get('base_table') == $old_base_table) {
|
Chris@0
|
265 $view->set('base_table', $new_base_table);
|
Chris@0
|
266 }
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 $this->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $old_base_table, $new_base_table) {
|
Chris@0
|
270 if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id && $handler_config['table'] == $old_base_table) {
|
Chris@0
|
271 $handler_config['table'] = $new_base_table;
|
Chris@0
|
272 }
|
Chris@0
|
273 });
|
Chris@0
|
274 }
|
Chris@0
|
275
|
Chris@0
|
276 /**
|
Chris@0
|
277 * Updates views if a data table is renamed.
|
Chris@0
|
278 *
|
Chris@0
|
279 * @param \Drupal\views\Entity\View[] $all_views
|
Chris@0
|
280 * All views.
|
Chris@0
|
281 * @param string $entity_type_id
|
Chris@0
|
282 * The entity type ID.
|
Chris@0
|
283 * @param string $old_data_table
|
Chris@0
|
284 * The old data table name.
|
Chris@0
|
285 * @param string $new_data_table
|
Chris@0
|
286 * The new data table name.
|
Chris@0
|
287 */
|
Chris@0
|
288 protected function dataTableRename($all_views, $entity_type_id, $old_data_table, $new_data_table) {
|
Chris@0
|
289 foreach ($all_views as $view) {
|
Chris@0
|
290 if ($view->get('base_table') == $old_data_table) {
|
Chris@0
|
291 $view->set('base_table', $new_data_table);
|
Chris@0
|
292 }
|
Chris@0
|
293 }
|
Chris@0
|
294
|
Chris@0
|
295 $this->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $old_data_table, $new_data_table) {
|
Chris@0
|
296 if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id && $handler_config['table'] == $old_data_table) {
|
Chris@0
|
297 $handler_config['table'] = $new_data_table;
|
Chris@0
|
298 }
|
Chris@0
|
299 });
|
Chris@0
|
300 }
|
Chris@0
|
301
|
Chris@0
|
302 /**
|
Chris@0
|
303 * Updates views if a data table is added.
|
Chris@0
|
304 *
|
Chris@0
|
305 * @param \Drupal\views\Entity\View[] $all_views
|
Chris@0
|
306 * All views.
|
Chris@0
|
307 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
Chris@0
|
308 * The entity type.
|
Chris@0
|
309 * @param string $new_data_table
|
Chris@0
|
310 * The new data table.
|
Chris@0
|
311 * @param string $base_table
|
Chris@0
|
312 * The base table.
|
Chris@0
|
313 */
|
Chris@0
|
314 protected function dataTableAddition($all_views, EntityTypeInterface $entity_type, $new_data_table, $base_table) {
|
Chris@0
|
315 /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */
|
Chris@0
|
316 $entity_type_id = $entity_type->id();
|
Chris@0
|
317 $storage = $this->entityManager->getStorage($entity_type_id);
|
Chris@0
|
318 $storage->setEntityType($entity_type);
|
Chris@0
|
319 $table_mapping = $storage->getTableMapping();
|
Chris@0
|
320 $data_table_fields = $table_mapping->getFieldNames($new_data_table);
|
Chris@0
|
321 $base_table_fields = $table_mapping->getFieldNames($base_table);
|
Chris@0
|
322
|
Chris@0
|
323 $data_table = $new_data_table;
|
Chris@0
|
324
|
Chris@0
|
325 $this->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $base_table, $data_table, $base_table_fields, $data_table_fields) {
|
Chris@0
|
326 if (isset($handler_config['entity_type']) && isset($handler_config['entity_field']) && $handler_config['entity_type'] == $entity_type_id) {
|
Chris@0
|
327 // Move all fields which just exists on the data table.
|
Chris@0
|
328 if ($handler_config['table'] == $base_table && in_array($handler_config['entity_field'], $data_table_fields) && !in_array($handler_config['entity_field'], $base_table_fields)) {
|
Chris@0
|
329 $handler_config['table'] = $data_table;
|
Chris@0
|
330 }
|
Chris@0
|
331 }
|
Chris@0
|
332 });
|
Chris@0
|
333 }
|
Chris@0
|
334
|
Chris@0
|
335 /**
|
Chris@0
|
336 * Updates views if a data table is removed.
|
Chris@0
|
337 *
|
Chris@0
|
338 * @param \Drupal\views\Entity\View[] $all_views
|
Chris@0
|
339 * All views.
|
Chris@0
|
340 * @param string $entity_type_id
|
Chris@0
|
341 * The entity type ID.
|
Chris@0
|
342 * @param string $old_data_table
|
Chris@0
|
343 * The name of the previous existing data table.
|
Chris@0
|
344 * @param string $base_table
|
Chris@0
|
345 * The name of the base table.
|
Chris@0
|
346 */
|
Chris@0
|
347 protected function dataTableRemoval($all_views, $entity_type_id, $old_data_table, $base_table) {
|
Chris@0
|
348 // We move back the data table back to the base table.
|
Chris@0
|
349 $this->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $old_data_table, $base_table) {
|
Chris@0
|
350 if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id) {
|
Chris@0
|
351 if ($handler_config['table'] == $old_data_table) {
|
Chris@0
|
352 $handler_config['table'] = $base_table;
|
Chris@0
|
353 }
|
Chris@0
|
354 }
|
Chris@0
|
355 });
|
Chris@0
|
356 }
|
Chris@0
|
357
|
Chris@0
|
358 /**
|
Chris@0
|
359 * Updates views if revision support is removed
|
Chris@0
|
360 *
|
Chris@0
|
361 * @param \Drupal\views\Entity\View[] $all_views
|
Chris@0
|
362 * All views.
|
Chris@0
|
363 * @param \Drupal\Core\Entity\EntityTypeInterface $original
|
Chris@0
|
364 * The origin entity type.
|
Chris@0
|
365 */
|
Chris@0
|
366 protected function revisionRemoval($all_views, EntityTypeInterface $original) {
|
Chris@0
|
367 $revision_base_table = $original->getRevisionTable();
|
Chris@0
|
368 $revision_data_table = $original->getRevisionDataTable();
|
Chris@0
|
369
|
Chris@0
|
370 foreach ($all_views as $view) {
|
Chris@0
|
371 if (in_array($view->get('base_table'), [$revision_base_table, $revision_data_table])) {
|
Chris@0
|
372 // Let's disable the views as we no longer support revisions.
|
Chris@0
|
373 $view->setStatus(FALSE);
|
Chris@0
|
374 }
|
Chris@0
|
375
|
Chris@0
|
376 // For any kind of field, let's rely on the broken handler functionality.
|
Chris@0
|
377 }
|
Chris@0
|
378 }
|
Chris@0
|
379
|
Chris@0
|
380 }
|