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