diff --git a/core/modules/field_ui/src/Tests/FieldUIDeleteTest.php b/core/modules/field_ui/src/Tests/FieldUIDeleteTest.php index 59469ac..164004f 100644 --- a/core/modules/field_ui/src/Tests/FieldUIDeleteTest.php +++ b/core/modules/field_ui/src/Tests/FieldUIDeleteTest.php @@ -91,13 +91,13 @@ 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 currently being deleted field is not shown in the entity deletions.'); // Delete the second field. $this->fieldUIDeleteField($bundle_path2, "node.$type_name2.$field_name", $field_label, $type_name2); diff --git a/core/modules/views/src/Entity/View.php b/core/modules/views/src/Entity/View.php index 1e9fac1..ff58457 100644 --- a/core/modules/views/src/Entity/View.php +++ b/core/modules/views/src/Entity/View.php @@ -7,6 +7,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Language\LanguageInterface; +use Drupal\views\Plugin\DependentWithRemovalPluginInterface; use Drupal\views\Views; use Drupal\views\ViewEntityInterface; @@ -272,6 +273,11 @@ public function calculateDependencies() { // Ensure that the view is dependant on the module that implements the view. $this->addDependency('module', $this->module); + // For disabled views don't add any handler specific dependencies. + if (!$this->status()) { + return $this; + } + $executable = $this->getExecutable(); $executable->initDisplay(); $executable->initStyle(); @@ -468,4 +474,87 @@ public function invalidateCaches() { \Drupal::service('cache_tags.invalidator')->invalidateTags($tags); } + /** + * {@inheritdoc} + */ + public function onDependencyRemoval(array $dependencies) { + $changed = FALSE; + $disable = TRUE; + + // Don't intervene if the views module is removed. + if (isset($dependencies['module']) && in_array('views', $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 and maybe don't + // need to disable this View. + unset($this->display[$display_id]['display_options'][$handler_types[$handler_type]['plural']][$handler_id]); + $disable = FALSE; + $changed = TRUE; + } + } + } + } + } + + // Check if the handlers have resolved all removed dependencies. + if ($changed) { + // Force the displays to be reinitialised to use the changed settings. + $this->getExecutable()->current_display = NULL; + $this->calculateDependencies(); + $new_dependencies = $this->getDependencies(); + foreach ($dependencies as $group => $dependency_list) { + foreach ($dependency_list as $config_key) { + if (isset($new_dependencies[$group]) && array_key_exists($config_key, $new_dependencies[$group])) { + // If any of the dependencies still exist in the new dependencies we + // will disable the view. + $disable = TRUE; + break 2; + } + } + } + } + + // Disable the View if we made no changes or the handlers were not able to + // remove the dependencies. This will cause all handler dependencies to be + // ignored on dependency calculation. + // @todo Display a message or add a 'disabled' fieldset that shows any + // disabled config. + if ($disable) { + $this->disable(); + $arguments = [ + '@id' => $this->id(), + ]; + $this->getLogger()->warning("View '@id': View was disabled because its settings depend on removed dependencies.", $arguments); + $changed = TRUE; + } + + $this->getExecutable()->setDisplay($current_display); + return $disable || $changed; + } + + /** + * Provides the 'system' channel logger service. + * + * @return \Psr\Log\LoggerInterface + * The 'system' channel logger. + */ + protected function getLogger() { + return \Drupal::logger('system'); + } + } diff --git a/core/modules/views/src/Plugin/DependentWithRemovalPluginInterface.php b/core/modules/views/src/Plugin/DependentWithRemovalPluginInterface.php new file mode 100644 index 0000000..5cca152 --- /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..1130198 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']; /** * {@inheritdoc} @@ -25,6 +26,15 @@ 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'); + } + + /** * Tests integration with image module. */ public function testImage() { @@ -69,8 +79,58 @@ public function testImage() { // Delete the 'foo' image style. $style->delete(); - // Checks that the view has been deleted too. - $this->assertNull(View::load('entity_test_fields')); + $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'])); + + } + + /** + * Tests removing a config dependency that disables the View. + */ + public function testConfigRemovalDisable() { + // Create a role we can add to the View and delete. + $role = Role::create(array( + '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 not been deleted too. + $this->assertNotNull($view); + + // Checks that the view has been disabled. + $this->assertFalse($view->status()); } }