diff --git a/core/modules/system/src/Tests/Entity/EntityDefinitionTestTrait.php b/core/modules/system/src/Tests/Entity/EntityDefinitionTestTrait.php index 77d84e7..5fb7f55 100644 --- a/core/modules/system/src/Tests/Entity/EntityDefinitionTestTrait.php +++ b/core/modules/system/src/Tests/Entity/EntityDefinitionTestTrait.php @@ -174,6 +174,9 @@ protected function removeEntityIndex() { $this->state->delete('entity_test_update.additional_entity_indexes'); } + /** + * Renames the base table to 'entity_test_update_new'. + */ protected function renameBaseTable() { $entity_type = clone $this->entityManager->getDefinition('entity_test_update'); @@ -183,6 +186,9 @@ protected function renameBaseTable() { $this->entityManager->clearCachedDefinitions(); } + /** + * Renames the data table to 'entity_test_update_data_new'. + */ protected function renameDataTable() { $entity_type = clone $this->entityManager->getDefinition('entity_test_update'); diff --git a/core/modules/views/src/EventSubscriber/ViewsEntitySchemaSubscriber.php b/core/modules/views/src/EventSubscriber/ViewsEntitySchemaSubscriber.php index b56094a..148d8df 100644 --- a/core/modules/views/src/EventSubscriber/ViewsEntitySchemaSubscriber.php +++ b/core/modules/views/src/EventSubscriber/ViewsEntitySchemaSubscriber.php @@ -23,16 +23,39 @@ class ViewsEntitySchemaSubscriber implements EventSubscriberInterface { use EntityTypeEventSubscriberTrait; use FieldStorageDefinitionEventSubscriberTrait; + /** + * Indicates that a base table got renamed. + */ const BASE_TABLE_RENAME = 0; + + /** + * Indicates that a data table got renamed. + */ const DATA_TABLE_RENAME = 1; + + /** + * Indicates that a data table got added. + */ const DATA_TABLE_ADDITION = 2; + + /** + * Indicates that a data table got removed. + */ const DATA_TABLE_REMOVAL = 3; /** + * The entity manager. + * * @var \Drupal\Core\Entity\EntityManagerInterface */ protected $entityManager; + /** + * Constructs a ViewsEntitySchemaSubscriber. + * + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager. + */ public function __construct(EntityManagerInterface $entity_manager) { $this->entityManager = $entity_manager; } @@ -101,6 +124,37 @@ public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeI * {@inheritdoc} */ public function onEntityTypeDelete(EntityTypeInterface $entity_type) { + $base_table = $entity_type->getBaseTable(); + $entity_type_id = $entity_type->id(); + + $all_views = $this->entityManager->getStorage('view')->loadMultiple(NULL); + /** @var \Drupal\views\Entity\View $view */ + foreach ($all_views as $id => $view) { + + // First check just the base table. + if ($view->get('base_table') == $base_table) { + $view->delete(); + unset($all_views[$id]); + } + + // Continue and look at all handlers and check whether the entity type + // disappears. + // @todo How can we ensure that we don't break views. + foreach (array_keys($view->get('display')) as $display_id) { + $display =& $view->getDisplay($display_id); + foreach (Views::getHandlerTypes() as $handler_type) { + $handler_type = $handler_type['plural']; + if (!isset($display['display_options'][$handler_type])) { + continue; + } + foreach ($display['display_options'][$handler_type] as &$handler_config) { + if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id) { + unset($handler_config); + } + } + } + } + } } /**