diff --git a/core/modules/field_ui/src/Tests/FieldUIDeleteTest.php b/core/modules/field_ui/src/Tests/FieldUIDeleteTest.php index 2704157..3b7cd74 100644 --- a/core/modules/field_ui/src/Tests/FieldUIDeleteTest.php +++ b/core/modules/field_ui/src/Tests/FieldUIDeleteTest.php @@ -5,6 +5,7 @@ use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\simpletest\WebTestBase; +use Drupal\views\Entity\View; use Drupal\views\Tests\ViewTestData; /** @@ -91,13 +92,13 @@ public function testDeleteField() { // Check the config dependencies of the first field. $this->drupalGet("$bundle_path2/fields/node.$type_name2.$field_name/delete"); - $this->assertText(t('The listed configuration will be deleted.')); + $this->assertText(t('The listed configuration will be updated.')); $this->assertText(t('View')); $this->assertText('test_view_field_delete'); $xml = $this->cssSelect('#edit-entity-deletes'); - // Remove the wrapping HTML. - $this->assertIdentical(FALSE, strpos($xml[0]->asXml(), $field_label), 'The currently being deleted field is not shown in the entity deletions.'); + // Test that nothing is scheduled for deletion. + $this->assertFalse(isset($xml[0]), 'The field currently being deleted is not shown in the entity deletions.'); // Delete the second field. $this->fieldUIDeleteField($bundle_path2, "node.$type_name2.$field_name", $field_label, $type_name2); @@ -106,6 +107,19 @@ public function testDeleteField() { $this->assertNull(FieldConfig::loadByName('node', $type_name2, $field_name), 'Field was deleted.'); // Check that the field storage was deleted too. $this->assertNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was deleted.'); + + // Test that the View isn't deleted and has been disabled. + $view = View::load('test_view_field_delete'); + $this->assertNotNull($view); + $this->assertFalse($view->status()); + // Test that the View no longer depends on the deleted field. + $expected_dependencies = [ + 'module' => [ + 0 => 'node', + 1 => 'user', + ], + ]; + $this->assertEqual($expected_dependencies, $view->getDependencies()); } } diff --git a/core/modules/views/src/Entity/View.php b/core/modules/views/src/Entity/View.php index a8d8d44..da1a04b 100644 --- a/core/modules/views/src/Entity/View.php +++ b/core/modules/views/src/Entity/View.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Language\LanguageInterface; +use Drupal\views\Plugin\DependentWithRemovalPluginInterface; use Drupal\views\Views; use Drupal\views\ViewEntityInterface; @@ -512,4 +513,61 @@ public function invalidateCaches() { \Drupal::service('cache_tags.invalidator')->invalidateTags($tags); } + /** + * {@inheritdoc} + */ + public function onDependencyRemoval(array $dependencies) { + $changed = FALSE; + + // Don't intervene if the views module is removed. + if (isset($dependencies['module']) && in_array('views', $dependencies['module'])) { + return FALSE; + } + + // If the base table for the View is provided by a module being removed, we + // delete the View because this is not something that can be fixed manually. + $views_data = Views::viewsData(); + $base_table = $this->get('base_table'); + $base_table_data = $views_data->get($base_table); + if (!empty($base_table_data['table']['provider']) && in_array($base_table_data['table']['provider'], $dependencies['module'])) { + return FALSE; + } + + $current_display = $this->getExecutable()->current_display; + $handler_types = Views::getHandlerTypes(); + + // Find all the handlers and check whether they want to do something on + // dependency removal. + foreach ($this->display as $display_id => $display_plugin_base) { + $this->getExecutable()->setDisplay($display_id); + $display = $this->getExecutable()->getDisplay(); + + foreach (array_keys($handler_types) as $handler_type) { + $handlers = $display->getHandlers($handler_type); + foreach ($handlers as $handler_id => $handler) { + if ($handler instanceof DependentWithRemovalPluginInterface) { + if ($handler->onDependencyRemoval($dependencies)) { + // Remove the handler and indicate we made changes. + unset($this->display[$display_id]['display_options'][$handler_types[$handler_type]['plural']][$handler_id]); + $changed = TRUE; + } + } + } + } + } + + // Disable the View if we made changes. + // @todo https://www.drupal.org/node/2832558 Give better feedback for + // disabled config. + if ($changed) { + // Force a recalculation of the dependencies if we made changes. + $this->getExecutable()->current_display = NULL; + $this->calculateDependencies(); + $this->disable(); + } + + $this->getExecutable()->setDisplay($current_display); + return $changed; + } + } diff --git a/core/modules/views/src/Plugin/DependentWithRemovalPluginInterface.php b/core/modules/views/src/Plugin/DependentWithRemovalPluginInterface.php new file mode 100644 index 0000000..59cb42d --- /dev/null +++ b/core/modules/views/src/Plugin/DependentWithRemovalPluginInterface.php @@ -0,0 +1,31 @@ +calculateDependencies(); + foreach ($current_dependencies as $group => $dependency_list) { + // Check if any of the handler dependencies match the dependencies being + // removed. + foreach ($dependency_list as $config_key) { + if (isset($dependencies[$group]) && array_key_exists($config_key, $dependencies[$group])) { + // This handlers dependency matches a dependency being removed, + // indicate that this handler needs to be removed. + $remove = TRUE; + break 2; + } + } + } + return $remove; + } + } diff --git a/core/modules/views/tests/src/Kernel/ViewsConfigDependenciesIntegrationTest.php b/core/modules/views/tests/src/Kernel/ViewsConfigDependenciesIntegrationTest.php index f5a2e39..294bb0f 100644 --- a/core/modules/views/tests/src/Kernel/ViewsConfigDependenciesIntegrationTest.php +++ b/core/modules/views/tests/src/Kernel/ViewsConfigDependenciesIntegrationTest.php @@ -5,6 +5,7 @@ use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\image\Entity\ImageStyle; +use Drupal\user\Entity\Role; use Drupal\views\Entity\View; /** @@ -17,7 +18,7 @@ class ViewsConfigDependenciesIntegrationTest extends ViewsKernelTestBase { /** * {@inheritdoc} */ - public static $modules = ['field', 'file', 'image', 'entity_test']; + public static $modules = ['field', 'file', 'image', 'entity_test', 'user', 'text']; /** * {@inheritdoc} @@ -25,6 +26,16 @@ class ViewsConfigDependenciesIntegrationTest extends ViewsKernelTestBase { public static $testViews = ['entity_test_fields']; /** + * {@inheritdoc} + */ + protected function setUp($import_test_views = TRUE) { + parent::setUp($import_test_views); + + $this->installEntitySchema('user'); + $this->installSchema('user', ['users_data']); + } + + /** * Tests integration with image module. */ public function testImage() { @@ -69,7 +80,81 @@ public function testImage() { // Delete the 'foo' image style. $style->delete(); + $view = View::load('entity_test_fields'); + + // Checks that the view has not been deleted too. + $this->assertNotNull(View::load('entity_test_fields')); + + // Checks that the image field was removed from the View. + $display = $view->getDisplay('default'); + $this->assertFalse(isset($display['display_options']['fields']['bar'])); + + // Checks that the view has been disabled. + $this->assertFalse($view->status()); + + $dependencies = $view->getDependencies() + ['config' => []]; + // Checks that the dependency on style 'foo' has been removed. + $this->assertFalse(in_array('image.style.foo', $dependencies['config'])); + } + + /** + * Tests removing a config dependency that deletes the View. + */ + public function testConfigRemovalRole() { + // Create a role we can add to the View and delete. + $role = Role::create([ + 'id' => 'dummy', + 'label' => 'dummy', + ]); + + $role->save(); + + /** @var \Drupal\views\ViewEntityInterface $view */ + $view = View::load('entity_test_fields'); + $display = &$view->getDisplay('default'); + + // Set the access to be restricted by the dummy role. + $display['display_options']['access'] = [ + 'type' => 'role', + 'options' => [ + 'role' => [ + $role->id() => $role->id(), + ], + ], + ]; + $view->save(); + + // Check that the View now has a dependency on the Role. + $dependencies = $view->getDependencies() + ['config' => []]; + $this->assertTrue(in_array('user.role.dummy', $dependencies['config'])); + + // Delete the role. + $role->delete(); + + $view = View::load('entity_test_fields'); + // Checks that the view has been deleted too. + $this->assertNull($view); + } + + /** + * Tests uninstalling a module that provides a base table for a View. + */ + public function testConfigRemovalBaseTable() { + // Find all the entity types provided by the entity_test module and install + // the schema for them so we can uninstall them. + $entities = \Drupal::entityTypeManager()->getDefinitions(); + foreach ($entities as $entity_type_id => $definition) { + if ($definition->getProvider() == 'entity_test') { + $this->installEntitySchema($entity_type_id); + }; + } + + // Check that removing the module that provides the base table for a View, + // deletes the View. + $this->assertNotNull(View::load('entity_test_fields')); + $this->container->get('module_installer')->uninstall(['entity_test']); + // Check that the View has been deleted. $this->assertNull(View::load('entity_test_fields')); }