diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index 1bef225..f196b3b 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -382,6 +382,31 @@ protected function addDependency($type, $name) { } /** + * Adds multiple dependencies. + * + * @param array $dependencies. + * An array of dependencies keyed by the type of dependency. One example: + * @code + * array( + * 'module' => array( + * 'node', + * 'field', + * 'image' + * ), + * ); + * @endcode + * + * @see ::addDependency + */ + protected function addDependencies(array $dependencies) { + foreach ($dependencies as $dependency_type => $list) { + foreach ($list as $name) { + $this->addDependency($dependency_type, $name); + } + } + } + + /** * {@inheritdoc} */ public function getConfigDependencyName() { diff --git a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php index 2628dfa..dbe76e7 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php +++ b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php @@ -882,4 +882,14 @@ function field_langcode(EntityInterface $entity) { } } + /** + * {@inheritdoc} + */ + public function getDependencies() { + // Add the module providing the configured field as a dependency. + $field = FieldHelper::fieldInfo()->getField($this->definition['entity_type'], $this->definition['field_name']); + return array($field->get('module')); + } + + } diff --git a/core/modules/views/lib/Drupal/views/Entity/View.php b/core/modules/views/lib/Drupal/views/Entity/View.php index 2beb7ac..33a4e9c 100644 --- a/core/modules/views/lib/Drupal/views/Entity/View.php +++ b/core/modules/views/lib/Drupal/views/Entity/View.php @@ -276,25 +276,39 @@ public function calculateDependencies() { $this->addDependency('module', $this->module); // Ensure that the view is dependant on the module that provides the schema // for the base table. - $schema = drupal_get_schema($this->base_table); + $schema = $this->drupalGetSchema($this->base_table); if ($this->module != $schema['module']) { $this->addDependency('module', $schema['module']); } + $plugin_types = ViewExecutable::getPluginTypes(); $handler_types = array(); foreach (ViewExecutable::getHandlerTypes() as $type) { $handler_types[] = $type['plural']; } foreach ($this->get('display') as $display) { + // Collect all dependencies of all handlers. foreach ($handler_types as $handler_type) { if (!empty($display['display_options'][$handler_type])) { foreach ($display['display_options'][$handler_type] as $handler) { + // Add the provider as dependency. if (isset($handler['provider']) && empty($handler['optional'])) { $this->addDependency('module', $handler['provider']); } + // Add the additional dependencies from the handler configuration. + if (isset($handler['dependencies'])) { + $this->addDependencies($handler['dependencies']); + } } } } + + // Collect all dependencies of plugins. + foreach ($plugin_types as $plugin_type) { + if (isset($display['display_options'][$plugin_type]['options']['dependencies'])) { + $this->addDependencies($display['display_options'][$plugin_type]['options']['dependencies']); + } + } } return $this->dependencies; } @@ -398,4 +412,11 @@ public function mergeDefaultDisplaysOptions() { $this->set('display', $displays); } + /** + * Wraps drupal_get_schema(). + */ + protected function drupalGetSchema($table = NULL, $rebuild = FALSE) { + return drupal_get_schema($table, $rebuild); + } + } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php index ba15dbb..f25e2d0 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php @@ -424,4 +424,16 @@ public static function preRenderFlattenData($form) { return $form; } + /** + * Returns an array of module dependencies for this plugin. + * + * Dependencies are a list of module names, which might depend on the + * configuration. + * + * @return array + */ + public function getDependencies() { + return array(); + } + } diff --git a/core/modules/views/lib/Drupal/views/ViewStorageInterface.php b/core/modules/views/lib/Drupal/views/ViewStorageInterface.php index 089f428..31b9f7b 100644 --- a/core/modules/views/lib/Drupal/views/ViewStorageInterface.php +++ b/core/modules/views/lib/Drupal/views/ViewStorageInterface.php @@ -29,4 +29,5 @@ public function &getDisplay($display_id); * Add defaults to the display options. */ public function mergeDefaultDisplaysOptions(); + } diff --git a/core/modules/views/tests/Drupal/views/Tests/Entity/ViewTest.php b/core/modules/views/tests/Drupal/views/Tests/Entity/ViewTest.php new file mode 100644 index 0000000..81aebfb --- /dev/null +++ b/core/modules/views/tests/Drupal/views/Tests/Entity/ViewTest.php @@ -0,0 +1,111 @@ + 'view')); + $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); + $entity_manager->expects($this->any()) + ->method('getDefinition') + ->will($this->returnValue($entity_definition)); + $container_builder = new ContainerBuilder(); + $container_builder->set('entity.manager', $entity_manager); + + // Setup the string translation. + $string_translation = $this->getStringTranslationStub(); + $container_builder->set('string_translation', $string_translation); + \Drupal::setContainer($container_builder); + } + + /** + * Tests calculating dependencies. + * + * @covers ::calculateDependencies + */ + public function testCalculateDependenciesWithNoDependencies() { + $values = array(); + $view = new TestView($values, 'view'); + $this->assertEquals(array('module' => array('node', 'views')), $view->calculateDependencies()); + } + + /** + * Tests calculating dependencies defined on some handlers. + * + * @covers ::calculateDependencies + */ + public function testCalculateDependenciesOnHandlers() { + $values = array(); + $values['display']['default']['display_options']['fields']['example']['dependencies'] = array(); + $values['display']['default']['display_options']['fields']['example2']['dependencies']['module'] = array('views', 'field'); + $values['display']['default']['display_options']['fields']['example3']['dependencies']['module'] = array('views', 'image'); + + $view = new TestView($values, 'view'); + $this->assertEquals(array('module' => array('field', 'image', 'node', 'views')), $view->calculateDependencies()); + } + + /** + * Tests calculating dependencies defined on some plugins. + * + * @covers ::calculateDependencies + */ + public function testCalculateDependenciesOnPlugins() { + $values = array(); + $values['display']['default']['display_options']['access']['options']['dependencies'] = array(); + $values['display']['default']['display_options']['row']['options']['dependencies']['module'] = array('views', 'field'); + $values['display']['default']['display_options']['style']['options']['dependencies']['module'] = array('views', 'image'); + + $view = new TestView($values, 'view'); + $this->assertEquals(array('module' => array('field', 'image', 'node', 'views')), $view->calculateDependencies()); + } + +} + +class TestView extends View { + + /** + * {@inheritdoc} + */ + protected function drupalGetSchema($table = NULL, $rebuild = FALSE) { + $result = array(); + if ($table == 'node') { + $result['module'] = 'node'; + } + return $result; + } + +} + +} + +namespace { + +if (!function_exists('t')) { + function t($string) { + return $string; + } +} + +} diff --git a/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ConfigHandler.php b/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ConfigHandler.php index 4f3bc7b..19241d4 100644 --- a/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ConfigHandler.php +++ b/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ConfigHandler.php @@ -238,6 +238,14 @@ public function submitForm(array &$form, array &$form_state) { // extra stuff on the form is not sent through. $handler->unpackOptions($handler->options, $options, NULL, FALSE); + // Add any dependencies as the handler is saved. Put it here so + // it does not need to be declared in defineOptions(). + if ($dependencies = $handler->getDependencies()) { + $handler->options['dependencies'] = $dependencies; + } + // Add the module providing the handler as dependency as well. + $handler->options['dependencies'][] = $handler->definition['provider']; + // Store the item back on the view $executable->setHandler($form_state['display_id'], $form_state['type'], $form_state['id'], $handler->options);