3 namespace Drupal\views\EventSubscriber;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Entity\EntityTypeEventSubscriberTrait;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\EntityTypeListenerInterface;
9 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
10 use Drupal\views\Views;
11 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14 * Reacts to changes on entity types to update all views entities.
16 class ViewsEntitySchemaSubscriber implements EntityTypeListenerInterface, EventSubscriberInterface {
18 use EntityTypeEventSubscriberTrait;
21 * Indicates that a base table got renamed.
23 const BASE_TABLE_RENAME = 0;
26 * Indicates that a data table got renamed.
28 const DATA_TABLE_RENAME = 1;
31 * Indicates that a data table got added.
33 const DATA_TABLE_ADDITION = 2;
36 * Indicates that a data table got removed.
38 const DATA_TABLE_REMOVAL = 3;
41 * Indicates that a revision table got renamed.
43 const REVISION_TABLE_RENAME = 4;
46 * Indicates that a revision table got added.
48 const REVISION_TABLE_ADDITION = 5;
51 * Indicates that a revision table got removed.
53 const REVISION_TABLE_REMOVAL = 6;
56 * Indicates that a revision data table got renamed.
58 const REVISION_DATA_TABLE_RENAME = 7;
61 * Indicates that a revision data table got added.
63 const REVISION_DATA_TABLE_ADDITION = 8;
66 * Indicates that a revision data table got removed.
68 const REVISION_DATA_TABLE_REMOVAL = 9;
73 * @var \Drupal\Core\Entity\EntityManagerInterface
75 protected $entityManager;
78 * Constructs a ViewsEntitySchemaSubscriber.
80 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
83 public function __construct(EntityManagerInterface $entity_manager) {
84 $this->entityManager = $entity_manager;
90 public static function getSubscribedEvents() {
91 return static::getEntityTypeEvents();
97 public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
100 // We implement a specific logic for table updates, which is bound to the
101 // default sql content entity storage.
102 if (!$this->entityManager->getStorage($entity_type->id()) instanceof SqlContentEntityStorage) {
106 if ($entity_type->getBaseTable() != $original->getBaseTable()) {
107 $changes[] = static::BASE_TABLE_RENAME;
110 $revision_add = $entity_type->isRevisionable() && !$original->isRevisionable();
111 $revision_remove = !$entity_type->isRevisionable() && $original->isRevisionable();
112 $translation_add = $entity_type->isTranslatable() && !$original->isTranslatable();
113 $translation_remove = !$entity_type->isTranslatable() && $original->isTranslatable();
116 $changes[] = static::REVISION_TABLE_ADDITION;
118 elseif ($revision_remove) {
119 $changes[] = static::REVISION_TABLE_REMOVAL;
121 elseif ($entity_type->isRevisionable() && $entity_type->getRevisionTable() != $original->getRevisionTable()) {
122 $changes[] = static::REVISION_TABLE_RENAME;
125 if ($translation_add) {
126 $changes[] = static::DATA_TABLE_ADDITION;
128 elseif ($translation_remove) {
129 $changes[] = static::DATA_TABLE_REMOVAL;
131 elseif ($entity_type->isTranslatable() && $entity_type->getDataTable() != $original->getDataTable()) {
132 $changes[] = static::DATA_TABLE_RENAME;
135 if ($entity_type->isRevisionable() && $entity_type->isTranslatable()) {
136 if ($revision_add || $translation_add) {
137 $changes[] = static::REVISION_DATA_TABLE_ADDITION;
139 elseif ($entity_type->getRevisionDataTable() != $original->getRevisionDataTable()) {
140 $changes[] = static::REVISION_DATA_TABLE_RENAME;
143 elseif ($original->isRevisionable() && $original->isTranslatable() && ($revision_remove || $translation_remove)) {
144 $changes[] = static::REVISION_DATA_TABLE_REMOVAL;
147 // Stop here if no changes are needed.
148 if (empty($changes)) {
152 /** @var \Drupal\views\Entity\View[] $all_views */
153 $all_views = $this->entityManager->getStorage('view')->loadMultiple(NULL);
155 foreach ($changes as $change) {
157 case static::BASE_TABLE_RENAME:
158 $this->baseTableRename($all_views, $entity_type->id(), $original->getBaseTable(), $entity_type->getBaseTable());
160 case static::DATA_TABLE_RENAME:
161 $this->dataTableRename($all_views, $entity_type->id(), $original->getDataTable(), $entity_type->getDataTable());
163 case static::DATA_TABLE_ADDITION:
164 $this->dataTableAddition($all_views, $entity_type, $entity_type->getDataTable(), $entity_type->getBaseTable());
166 case static::DATA_TABLE_REMOVAL:
167 $this->dataTableRemoval($all_views, $entity_type->id(), $original->getDataTable(), $entity_type->getBaseTable());
169 case static::REVISION_TABLE_RENAME:
170 $this->baseTableRename($all_views, $entity_type->id(), $original->getRevisionTable(), $entity_type->getRevisionTable());
172 case static::REVISION_TABLE_ADDITION:
173 // If we add revision support we don't have to do anything.
175 case static::REVISION_TABLE_REMOVAL:
176 $this->revisionRemoval($all_views, $original);
178 case static::REVISION_DATA_TABLE_RENAME:
179 $this->dataTableRename($all_views, $entity_type->id(), $original->getRevisionDataTable(), $entity_type->getRevisionDataTable());
181 case static::REVISION_DATA_TABLE_ADDITION:
182 $this->dataTableAddition($all_views, $entity_type, $entity_type->getRevisionDataTable(), $entity_type->getRevisionTable());
184 case static::REVISION_DATA_TABLE_REMOVAL:
185 $this->dataTableRemoval($all_views, $entity_type->id(), $original->getRevisionDataTable(), $entity_type->getRevisionTable());
190 foreach ($all_views as $view) {
191 // All changes done to the views here can be trusted and this might be
192 // called during updates, when it is not safe to rely on configuration
193 // containing valid schema. Trust the data and disable schema validation
195 $view->trustData()->save();
202 public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
204 $entity_type->getBaseTable(),
205 $entity_type->getDataTable(),
206 $entity_type->getRevisionTable(),
207 $entity_type->getRevisionDataTable(),
210 $all_views = $this->entityManager->getStorage('view')->loadMultiple(NULL);
211 /** @var \Drupal\views\Entity\View $view */
212 foreach ($all_views as $id => $view) {
214 // First check just the base table.
215 if (in_array($view->get('base_table'), $tables)) {
223 * Applies a callable onto all handlers of all passed in views.
225 * @param \Drupal\views\Entity\View[] $all_views
226 * All views entities.
227 * @param callable $process
228 * A callable which retrieves a handler config array.
230 protected function processHandlers(array $all_views, callable $process) {
231 foreach ($all_views as $view) {
232 foreach (array_keys($view->get('display')) as $display_id) {
233 $display = &$view->getDisplay($display_id);
234 foreach (Views::getHandlerTypes() as $handler_type) {
235 $handler_type = $handler_type['plural'];
236 if (!isset($display['display_options'][$handler_type])) {
239 foreach ($display['display_options'][$handler_type] as $id => &$handler_config) {
240 $process($handler_config);
241 if ($handler_config === NULL) {
242 unset($display['display_options'][$handler_type][$id]);
251 * Updates views if a base table is renamed.
253 * @param \Drupal\views\Entity\View[] $all_views
255 * @param string $entity_type_id
256 * The entity type ID.
257 * @param string $old_base_table
258 * The old base table name.
259 * @param string $new_base_table
260 * The new base table name.
262 protected function baseTableRename($all_views, $entity_type_id, $old_base_table, $new_base_table) {
263 foreach ($all_views as $view) {
264 if ($view->get('base_table') == $old_base_table) {
265 $view->set('base_table', $new_base_table);
269 $this->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $old_base_table, $new_base_table) {
270 if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id && $handler_config['table'] == $old_base_table) {
271 $handler_config['table'] = $new_base_table;
277 * Updates views if a data table is renamed.
279 * @param \Drupal\views\Entity\View[] $all_views
281 * @param string $entity_type_id
282 * The entity type ID.
283 * @param string $old_data_table
284 * The old data table name.
285 * @param string $new_data_table
286 * The new data table name.
288 protected function dataTableRename($all_views, $entity_type_id, $old_data_table, $new_data_table) {
289 foreach ($all_views as $view) {
290 if ($view->get('base_table') == $old_data_table) {
291 $view->set('base_table', $new_data_table);
295 $this->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $old_data_table, $new_data_table) {
296 if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id && $handler_config['table'] == $old_data_table) {
297 $handler_config['table'] = $new_data_table;
303 * Updates views if a data table is added.
305 * @param \Drupal\views\Entity\View[] $all_views
307 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
309 * @param string $new_data_table
310 * The new data table.
311 * @param string $base_table
314 protected function dataTableAddition($all_views, EntityTypeInterface $entity_type, $new_data_table, $base_table) {
315 /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */
316 $entity_type_id = $entity_type->id();
317 $storage = $this->entityManager->getStorage($entity_type_id);
318 $storage->setEntityType($entity_type);
319 $table_mapping = $storage->getTableMapping();
320 $data_table_fields = $table_mapping->getFieldNames($new_data_table);
321 $base_table_fields = $table_mapping->getFieldNames($base_table);
323 $data_table = $new_data_table;
325 $this->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $base_table, $data_table, $base_table_fields, $data_table_fields) {
326 if (isset($handler_config['entity_type']) && isset($handler_config['entity_field']) && $handler_config['entity_type'] == $entity_type_id) {
327 // Move all fields which just exists on the data table.
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)) {
329 $handler_config['table'] = $data_table;
336 * Updates views if a data table is removed.
338 * @param \Drupal\views\Entity\View[] $all_views
340 * @param string $entity_type_id
341 * The entity type ID.
342 * @param string $old_data_table
343 * The name of the previous existing data table.
344 * @param string $base_table
345 * The name of the base table.
347 protected function dataTableRemoval($all_views, $entity_type_id, $old_data_table, $base_table) {
348 // We move back the data table back to the base table.
349 $this->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $old_data_table, $base_table) {
350 if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id) {
351 if ($handler_config['table'] == $old_data_table) {
352 $handler_config['table'] = $base_table;
359 * Updates views if revision support is removed
361 * @param \Drupal\views\Entity\View[] $all_views
363 * @param \Drupal\Core\Entity\EntityTypeInterface $original
364 * The origin entity type.
366 protected function revisionRemoval($all_views, EntityTypeInterface $original) {
367 $revision_base_table = $original->getRevisionTable();
368 $revision_data_table = $original->getRevisionDataTable();
370 foreach ($all_views as $view) {
371 if (in_array($view->get('base_table'), [$revision_base_table, $revision_data_table])) {
372 // Let's disable the views as we no longer support revisions.
373 $view->setStatus(FALSE);
376 // For any kind of field, let's rely on the broken handler functionality.