diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index ec90b7e..b7c271b 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -385,6 +385,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..96098cd 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,13 @@ function field_langcode(EntityInterface $entity) { } } + /** + * {@inheritdoc} + */ + public function getDependencies() { + // Add the module providing the configured field as a dependency. + return array('entity' => array($this->field_info->getConfigDependencyName())); + } + + } diff --git a/core/modules/views/lib/Drupal/views/Entity/View.php b/core/modules/views/lib/Drupal/views/Entity/View.php index 304d1f8..c3e19aa 100644 --- a/core/modules/views/lib/Drupal/views/Entity/View.php +++ b/core/modules/views/lib/Drupal/views/Entity/View.php @@ -12,7 +12,6 @@ use Drupal\views\Views; use Drupal\views_ui\ViewUI; use Drupal\views\ViewStorageInterface; -use Drupal\views\ViewExecutable; /** * Defines a View configuration entity class. @@ -273,28 +272,43 @@ public function calculateDependencies() { // Ensure that the view is dependant on the module that implements the view. $this->addDependency('module', $this->module); - // Ensure that the view is dependant on the module that provides the schema + // Ensure that the view is dependent 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']); } $handler_types = array(); - foreach (ViewExecutable::getHandlerTypes() as $type) { + foreach (Views::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 (!empty($handler['dependencies'])) { + $this->addDependencies($handler['dependencies']); + } } } } + + // Collect all dependencies of plugins. + foreach (Views::getPluginTypes('plugin') as $plugin_type) { + if (!empty($display['display_options'][$plugin_type]['options']['dependencies'])) { + $this->addDependencies($display['display_options'][$plugin_type]['options']['dependencies']); + } + } } + return $this->dependencies; } @@ -386,4 +400,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/HandlerBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php index 6196fc3..aae8e27 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php @@ -152,6 +152,7 @@ protected function defineOptions() { $options['relationship'] = array('default' => 'none'); $options['group_type'] = array('default' => 'group'); $options['admin_label'] = array('default' => '', 'translatable' => TRUE); + $options['dependencies'] = array('default' => array()); return $options; } 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/Plugin/views/wizard/WizardPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php index fc9950a..7dbe0c1 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php @@ -761,6 +761,7 @@ protected function defaultDisplayOptions() { foreach ($display_options as &$options) { $options['options'] = array(); $options['provider'] = 'views'; + $options['dependencies'] = array(); } // Add a least one field so the view validates and the user has a preview. 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..0aae557 --- /dev/null +++ b/core/modules/views/tests/Drupal/views/Tests/Entity/ViewTest.php @@ -0,0 +1,97 @@ + 'View entity test', + 'description' => 'Tests the \Drupal\views\Entity\View class.', + 'group' => 'Views', + ); + } + + /** + * {@inheritdoc} + */ + protected function setUp() { + + // Setup the entity manager. + $entity_definition = new EntityType(array('id' => '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 + * @dataProvider calculateDependenciesProvider + */ + public function testCalculateDependencies($values, $deps) { + $view = new TestView($values, 'view'); + $this->assertEquals(array('module' => $deps), $view->calculateDependencies()); + } + + public function calculateDependenciesProvider(){ + $handler['display']['default']['display_options']['fields']['example']['dependencies'] = array(); + $handler['display']['default']['display_options']['fields']['example2']['dependencies']['module'] = array('views', 'field'); + $handler['display']['default']['display_options']['fields']['example3']['dependencies']['module'] = array('views', 'image'); + + $plugin['display']['default']['display_options']['access']['options']['dependencies'] = array(); + $plugin['display']['default']['display_options']['row']['options']['dependencies']['module'] = array('views', 'field'); + $plugin['display']['default']['display_options']['style']['options']['dependencies']['module'] = array('views', 'image'); + + return array( + array(array(), array('node', 'views')), + array($handler, array('field', 'image', 'node', 'views')), + array($plugin, array('field', 'image', 'node', 'views')), + ); + } +} + +class TestView extends View { + + /** + * {@inheritdoc} + */ + protected function drupalGetSchema($table = NULL, $rebuild = FALSE) { + $result = array(); + if ($table == 'node') { + $result['module'] = 'node'; + } + return $result; + } + +} + +} 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..8533e69 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 a dependency as well. + $handler->options['dependencies']['module'][] = $handler->definition['provider']; + // Store the item back on the view $executable->setHandler($form_state['display_id'], $form_state['type'], $form_state['id'], $handler->options);