diff --git a/core/modules/views/src/Entity/View.php b/core/modules/views/src/Entity/View.php index 1ad685a..ddd90d0 100644 --- a/core/modules/views/src/Entity/View.php +++ b/core/modules/views/src/Entity/View.php @@ -100,6 +100,13 @@ class View extends ConfigEntityBase implements ViewStorageInterface { protected $executable; /** + * The views data. + * + * @var \Drupal\views\ViewsData + */ + protected $viewsData; + + /** * The module implementing this view. * * @var string @@ -122,6 +129,18 @@ public function getExecutable() { } /** + * Gets the views data. + * + * @return \Drupal\views\ViewsData + */ + protected function getViewsData() { + if (!isset($this->viewsData)) { + $this->viewsData = Views::viewsData(); + } + return $this->viewsData; + } + + /** * Overrides Drupal\Core\Config\Entity\ConfigEntityBase::createDuplicate(). */ public function createDuplicate() { @@ -250,12 +269,10 @@ public function calculateDependencies() { $this->addDependency('module', $this->module); // Ensure that the view is dependent on the module that provides the schema // for the base table. - $schema = $this->drupalGetSchema($this->base_table); - // @todo Entity base tables are no longer registered in hook_schema(). Once - // we automate the views data for entity types add the entity type - // type provider as a dependency. See https://drupal.org/node/1740492. - if ($schema && $this->module != $schema['module']) { - $this->addDependency('module', $schema['module']); + + $views_data = $this->getViewsData()->get($this->get('base_table')); + if (!empty($views_data['table']['provider'])) { + $this->addDependency('module', $views_data['table']['provider']); } $handler_types = array(); @@ -383,11 +400,4 @@ 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/src/EntityViewsData.php b/core/modules/views/src/EntityViewsData.php index b2859d0..8593ce9 100644 --- a/core/modules/views/src/EntityViewsData.php +++ b/core/modules/views/src/EntityViewsData.php @@ -129,6 +129,7 @@ public function getViewsData() { // Setup base information of the views data. $data[$base_table]['table']['entity type'] = $this->entityType->id(); $data[$base_table]['table']['group'] = $this->entityType->getLabel(); + $data[$base_table]['table']['provider'] = $this->entityType->getProvider(); $data[$base_table]['table']['base'] = [ 'field' => $base_field, 'title' => $this->entityType->getLabel(), @@ -157,10 +158,12 @@ public function getViewsData() { ]; $data[$data_table]['table']['entity type'] = $this->entityType->id(); $data[$data_table]['table']['group'] = $this->entityType->getLabel(); + $data[$data_table]['table']['provider'] = $this->entityType->getProvider(); } if ($revision_table) { $data[$revision_table]['table']['entity type'] = $this->entityType->id(); $data[$revision_table]['table']['group'] = $this->t('@entity_type revision', ['@entity_type' => $this->entityType->getLabel()]); + $data[$revision_table]['table']['provider'] = $this->entityType->getProvider(); $data[$revision_table]['table']['base'] = array( 'field' => $revision_field, 'title' => $this->t('@entity_type revisions', array('@entity_type' => $this->entityType->getLabel())), diff --git a/core/modules/views/src/Plugin/views/query/QueryPluginBase.php b/core/modules/views/src/Plugin/views/query/QueryPluginBase.php index 4097efa..7b8a35b 100644 --- a/core/modules/views/src/Plugin/views/query/QueryPluginBase.php +++ b/core/modules/views/src/Plugin/views/query/QueryPluginBase.php @@ -118,6 +118,21 @@ public function summaryTitle() { } /** + * {@inheritdoc} + */ + public function getDependencies() { + $dependencies = []; + foreach ($this->view->getBaseTables() as $base_table) { + $views_data = Views::viewsData()->get($base_table); + if (!empty($views_data['table']['provider'])) { + $dependencies['module'][] = $views_data['table']['provider']; + } + } + + return $dependencies; + } + + /** * Set a LIMIT on the query, specifying a maximum number of results. */ public function setLimit($limit) { diff --git a/core/modules/views/tests/Drupal/views/Tests/EntityViewsDataTest.php b/core/modules/views/tests/Drupal/views/Tests/EntityViewsDataTest.php index 4618309..c9de1eb 100644 --- a/core/modules/views/tests/Drupal/views/Tests/EntityViewsDataTest.php +++ b/core/modules/views/tests/Drupal/views/Tests/EntityViewsDataTest.php @@ -34,7 +34,7 @@ class EntityViewsDataTest extends UnitTestCase { /** * Entity info to use in this test. * - * @var \Drupal\Core\Entity\EntityTypeInterface + * @var \Drupal\Core\Entity\EntityTypeInterface|\Drupal\views\Tests\TestEntityType */ protected $baseEntityType; @@ -87,6 +87,7 @@ protected function setUp() { 'id' => 'entity_test', 'label' => 'Entity test', 'entity_keys' => ['id' => 'id'], + 'provider' => 'entity_test', ]); $this->translationManager = $this->getStringTranslationStub(); @@ -134,6 +135,7 @@ public function testBaseTables() { $this->assertEquals('entity_test', $data['entity_test']['table']['entity type']); $this->assertEquals('Entity test', $data['entity_test']['table']['group']); + $this->assertEquals('entity_test', $data['entity_test']['table']['provider']); $this->assertEquals('id', $data['entity_test']['table']['base']['field']); $this->assertEquals('Entity test', $data['entity_test']['table']['base']['title']); @@ -162,6 +164,7 @@ public function testDataTable() { $this->assertEquals('entity_test_mul', $data['entity_test_mul_property_data']['table']['entity type']); $this->assertEquals('Entity test', $data['entity_test_mul_property_data']['table']['group']); + $this->assertEquals('entity_test', $data['entity_test']['table']['provider']); $this->assertEquals(['field' => 'label', 'table' => 'entity_test_mul_property_data'], $data['entity_test']['table']['base']['defaults']); // Ensure the join information is set up properly. @@ -188,6 +191,7 @@ public function testRevisionTable() { $this->assertEquals('entity_test_mulrev', $data['entity_test_mulrev_revision']['table']['entity type']); $this->assertEquals('entity_test_mulrev', $data['entity_test_mulrev_property_revision']['table']['entity type']); $this->assertEquals('Entity test revision', $data['entity_test_mulrev_revision']['table']['group']); + $this->assertEquals('entity_test', $data['entity_test']['table']['provider']); // Ensure the join information is set up properly. // Tests the join definition between the base and the revision table. diff --git a/core/modules/views/tests/src/Unit/Entity/ViewTest.php b/core/modules/views/tests/src/Unit/Entity/ViewTest.php index 8af4e6d..401b666 100644 --- a/core/modules/views/tests/src/Unit/Entity/ViewTest.php +++ b/core/modules/views/tests/src/Unit/Entity/ViewTest.php @@ -32,6 +32,19 @@ protected function setUp() { $container_builder = new ContainerBuilder(); $container_builder->set('entity.manager', $entity_manager); + $views_data = $this->getMockBuilder('Drupal\views\ViewsData') + ->disableOriginalConstructor() + ->getMock(); + $views_data->expects($this->once()) + ->method('get') + ->with('node') + ->willReturn([ + 'table' => ['provider' => 'node'] + ]); + + $container_builder->set('entity.manager', $entity_manager); + $container_builder->set('views.views_data', $views_data); + // Setup the string translation. $string_translation = $this->getStringTranslationStub(); $container_builder->set('string_translation', $string_translation); @@ -50,14 +63,18 @@ public function testCalculateDependencies($values, $deps) { } public function calculateDependenciesProvider(){ + $handler['base_table'] = 'node'; $handler['display']['default']['provider'] = 'block'; $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'); + $handler['display']['default']['display_options']['query']['options']['dependencies'] = ['module' => ['node']]; + $plugin['base_table'] = 'node'; $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'); + $plugin['display']['default']['display_options']['query']['options']['dependencies'] = ['module' => ['node']]; return array( array(array(), array('node', 'views')), @@ -69,17 +86,6 @@ public function calculateDependenciesProvider(){ class TestView extends View { - /** - * {@inheritdoc} - */ - protected function drupalGetSchema($table = NULL, $rebuild = FALSE) { - $result = array(); - if ($table == 'node') { - $result['module'] = 'node'; - } - return $result; - } - } }