diff --git a/core/modules/entity_reference/entity_reference.views.inc b/core/modules/entity_reference/entity_reference.views.inc index 486fca131ac8..191005f1844a 100644 --- a/core/modules/entity_reference/entity_reference.views.inc +++ b/core/modules/entity_reference/entity_reference.views.inc @@ -13,45 +13,59 @@ use Drupal\field\FieldStorageConfigInterface; function entity_reference_field_views_data(FieldStorageConfigInterface $field_storage) { $data = views_field_default_views_data($field_storage); $entity_manager = \Drupal::entityManager(); - $table_mapping = $entity_manager->getStorage($field_storage->getTargetEntityTypeId())->getTableMapping(); foreach ($data as $table_name => $table_data) { // Add a relationship to the target entity type. $target_entity_type_id = $field_storage->getSetting('target_type'); $target_entity_type = $entity_manager->getDefinition($target_entity_type_id); - $target_base_table = $target_entity_type->getBaseTable(); + $entity_type_id = $field_storage->getTargetEntityTypeId(); + $entity_type = $entity_manager->getDefinition($entity_type_id); + $target_base_table = $target_entity_type->getDataTable() ?: $target_entity_type->getBaseTable(); + $field_name = $field_storage->getName(); // Provide a relationship for the entity type with the entity reference // field. $args = array( '@label' => $target_entity_type->getLabel(), - '@field_name' => $field_storage->getName(), + '@field_name' => $field_name, ); - $data[$table_name][$field_storage->getName()]['relationship'] = array( + $data[$table_name][$field_name]['relationship'] = array( + 'title' => t('@label referenced from @field_name', $args), + 'label' => t('@field_name: @label', $args), + 'group' => $entity_type->getLabel(), + 'help' => t('Appears in: @bundles.', array('@bundles' => implode(', ', $field_storage->getBundles()))), 'id' => 'standard', 'base' => $target_base_table, 'entity type' => $target_entity_type_id, 'base field' => $target_entity_type->getKey('id'), - 'relationship field' => $field_storage->getName() . '_target_id', - 'title' => t('@label referenced from @field_name', $args), - 'label' => t('@field_name: @label', $args), + 'relationship field' => $field_name . '_target_id', ); // Provide a reverse relationship for the entity type that is referenced by // the field. - $entity_type_id = $field_storage->getTargetEntityTypeId(); - $pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_storage->getName(); + $args['@entity'] = $entity_type->getLabel(); + $args['@label'] = $target_entity_type->getLowercaseLabel(); + $pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_name; /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ - $table_mapping = \Drupal::entityManager()->getStorage($entity_type_id)->getTableMapping(); + $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping(); $data[$target_base_table][$pseudo_field_name]['relationship'] = array( - 'title' => t('@label using @field_name', $args), - 'help' => t('Relate each @label with a @field_name.', $args), + 'title' => t('@entity using @field_name', $args), + 'label' => t('@field_name', array('@field_name' => $field_name)), + 'group' => $target_entity_type->getLabel(), + 'help' => t('Relate each @entity with a @field_name set to the @label.', $args), 'id' => 'entity_reverse', - 'field_name' => $field_storage->getName(), + 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(), + 'entity_type' => $entity_type_id, + 'base field' => $entity_type->getKey('id'), + 'field_name' => $field_name, 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), - 'field field' => $field_storage->getName() . '_target_id', - 'base' => $target_entity_type->getBaseTable(), - 'base field' => $target_entity_type->getKey('id'), - 'label' => t('@field_name', array('@field_name' => $field_storage->getName())), + 'field field' => $field_name . '_target_id', + 'join_extra' => array( + array( + 'field' => 'deleted', + 'value' => 0, + 'numeric' => TRUE, + ), + ), ); } diff --git a/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php b/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php index f2a803dd3642..4190f174b606 100644 --- a/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php +++ b/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php @@ -7,9 +7,9 @@ namespace Drupal\entity_reference\Tests\Views; -use Drupal\Core\Field\FieldStorageDefinitionInterface; -use Drupal\field\Entity\FieldStorageConfig; -use Drupal\field\Entity\FieldConfig; +use Drupal\entity_reference\Tests\EntityReferenceTestTrait; +use Drupal\entity_test\Entity\EntityTest; +use Drupal\entity_test\Entity\EntityTestMul; use Drupal\views\Tests\ViewTestData; use Drupal\views\Tests\ViewUnitTestBase; use Drupal\views\Views; @@ -22,12 +22,19 @@ use Drupal\views\Views; */ class EntityReferenceRelationshipTest extends ViewUnitTestBase { + use EntityReferenceTestTrait; + /** * Views used by this test. * * @var array */ - public static $testViews = array('test_entity_reference_view'); + public static $testViews = array( + 'test_entity_reference_entity_test_view', + 'test_entity_reference_reverse_entity_test_view', + 'test_entity_reference_entity_test_mul_view', + 'test_entity_reference_reverse_entity_test_mul_view', + ); /** * Modules to install. @@ -51,103 +58,174 @@ class EntityReferenceRelationshipTest extends ViewUnitTestBase { $this->installEntitySchema('user'); $this->installEntitySchema('entity_test'); + $this->installEntitySchema('entity_test_mul'); - $field_storage = FieldStorageConfig::create(array( - 'entity_type' => 'entity_test', - 'field_name' => 'field_test', - 'type' => 'entity_reference', - 'settings' => array( - 'target_type' => 'entity_test', - ), - 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, - )); - $field_storage->save(); + // Create reference from entity_test to entity_test_mul. + $this->createEntityReferenceField('entity_test','entity_test','field_test_data','field_test_data','entity_test_mul'); - $field = FieldConfig::create(array( - 'entity_type' => 'entity_test', - 'field_name' => 'field_test', - 'bundle' => 'entity_test', - 'settings' => array( - 'handler' => 'default', - 'handler_settings' => array(), - ), - )); - $field->save(); + // Create reference from entity_test_mul to entity_test. + $this->createEntityReferenceField('entity_test_mul', 'entity_test_mul', 'field_data_test', 'field_data_test', 'entity_test'); ViewTestData::createTestViews(get_class($this), array('entity_reference_test_views')); - - // Create some test entities which link each other. - $entity_storage= \Drupal::entityManager()->getStorage('entity_test'); - $referenced_entity = $entity_storage->create(array()); - $referenced_entity->save(); - $this->entities[$referenced_entity->id()] = $referenced_entity; - - $entity = $entity_storage->create(array()); - $entity->field_test->target_id = $referenced_entity->id(); - $entity->save(); - $this->assertEqual($entity->field_test[0]->entity->id(), $referenced_entity->id()); - $this->entities[$entity->id()] = $entity; - - $entity = $entity_storage->create(array()); - $entity->field_test->target_id = $referenced_entity->id(); - $entity->save(); - $this->assertEqual($entity->field_test[0]->entity->id(), $referenced_entity->id()); - $this->entities[$entity->id()] = $entity; - - Views::viewsData()->clear(); } /** * Tests using the views relationship. */ - public function testRelationship() { - // Check just the generated views data. - $views_data_field_test = Views::viewsData()->get('entity_test__field_test'); - $this->assertEqual($views_data_field_test['field_test']['relationship']['id'], 'standard'); - $this->assertEqual($views_data_field_test['field_test']['relationship']['base'], 'entity_test'); - $this->assertEqual($views_data_field_test['field_test']['relationship']['base field'], 'id'); - $this->assertEqual($views_data_field_test['field_test']['relationship']['relationship field'], 'field_test_target_id'); + public function testNoDataTableRelationship() { + + // Create some test entities which link each other. + $referenced_entity = EntityTestMul::create(); + $referenced_entity->save(); + + $entity = EntityTest::create(); + $entity->field_test_data->target_id = $referenced_entity->id(); + $entity->save(); + $this->assertEqual($entity->field_test_data[0]->entity->id(), $referenced_entity->id()); + $this->entities[] = $entity; + + $entity = EntityTest::create(); + $entity->field_test_data->target_id = $referenced_entity->id(); + $entity->save(); + $this->assertEqual($entity->field_test_data[0]->entity->id(), $referenced_entity->id()); + $this->entities[] = $entity; + + Views::viewsData()->clear(); + + // Check the generated views data. + $views_data = Views::viewsData()->get('entity_test__field_test_data'); + $this->assertEqual($views_data['field_test_data']['relationship']['id'], 'standard'); + $this->assertEqual($views_data['field_test_data']['relationship']['base'], 'entity_test_mul_property_data'); + $this->assertEqual($views_data['field_test_data']['relationship']['base field'], 'id'); + $this->assertEqual($views_data['field_test_data']['relationship']['relationship field'], 'field_test_data_target_id'); + $this->assertEqual($views_data['field_test_data']['relationship']['entity type'], 'entity_test_mul'); // Check the backwards reference. - $views_data_entity_test = Views::viewsData()->get('entity_test'); - $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['id'], 'entity_reverse'); - $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['base'], 'entity_test'); - $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['base field'], 'id'); - $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['field table'], 'entity_test__field_test'); - $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['field field'], 'field_test_target_id'); - + $views_data = Views::viewsData()->get('entity_test_mul_property_data'); + $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['id'], 'entity_reverse'); + $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['base'], 'entity_test'); + $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['base field'], 'id'); + $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['field table'], 'entity_test__field_test_data'); + $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['field field'], 'field_test_data_target_id'); + $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['field_name'], 'field_test_data'); + $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['entity_type'], 'entity_test'); + $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['join_extra'][0], ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE]); // Check an actual test view. - $view = Views::getView('test_entity_reference_view'); + $view = Views::getView('test_entity_reference_entity_test_view'); $this->executeView($view); - - foreach (array_keys($view->result) as $index) { + /** @var \Drupal\views\ResultRow $row */ + foreach ($view->result as $index => $row) { // Check that the actual ID of the entity is the expected one. - $this->assertEqual($view->result[$index]->id, $this->entities[$index + 1]->id()); + $this->assertEqual($row->id, $this->entities[$index]->id()); // Also check that we have the correct result entity. - $this->assertEqual($view->result[$index]->_entity->id(), $this->entities[$index + 1]->id()); + $this->assertEqual($row->_entity->id(), $this->entities[$index]->id()); // Test the forward relationship. - // The second and third entity refer to the first one. - // The value key on the result will be in the format - // BASE_TABLE_FIELD_NAME. - $this->assertEqual($view->result[$index]->entity_test_entity_test__field_test_id, $index == 0 ? NULL : 1); + $this->assertEqual($row->entity_test_mul_property_data_entity_test__field_test_data_i, 1); + + // Test that the correct relationship entity is on the row. + $this->assertEqual($row->_relationship_entities['field_test_data']->id(), 1); + $this->assertEqual($row->_relationship_entities['field_test_data']->bundle(), 'entity_test_mul'); - if ($index > 0) { - // Test that the correct relationship entity is on the row. - $this->assertEqual($view->result[$index]->_relationship_entities['test_relationship']->id(), 1); - } } - $view->destroy(); - $this->executeView($view, 'embed_1'); + // Check the backwards reference view. + $view = Views::getView('test_entity_reference_reverse_entity_test_view'); + $this->executeView($view); + /** @var \Drupal\views\ResultRow $row */ + foreach ($view->result as $index => $row) { + $this->assertEqual($row->id, 1); + $this->assertEqual($row->_entity->id(), 1); - foreach (array_keys($view->result) as $index) { - $this->assertEqual($view->result[$index]->id, $this->entities[$index + 1]->id()); - $this->assertEqual($view->result[$index]->_entity->id(), $this->entities[$index + 1]->id()); - // The second and third entity refer to the first one. - $this->assertEqual($view->result[$index]->entity_test_entity_test__field_test_id, $index == 0 ? NULL : 1); + // Test the backwards relationship. + $this->assertEqual($row->field_test_data_entity_test_mul_property_data_id, $this->entities[$index]->id()); + + // Test that the correct relationship entity is on the row. + $this->assertEqual($row->_relationship_entities['reverse__entity_test__field_test_data']->id(), $this->entities[$index]->id()); + $this->assertEqual($row->_relationship_entities['reverse__entity_test__field_test_data']->bundle(), 'entity_test'); + } + } + + /** + * Tests views data generated for relationship. + * + * @see entity_reference_field_views_data() + */ + public function testDataTableRelationship() { + + // Create some test entities which link each other. + $referenced_entity = EntityTest::create(); + $referenced_entity->save(); + + $entity = EntityTestMul::create(); + $entity->field_data_test->target_id = $referenced_entity->id(); + $entity->save(); + $this->assertEqual($entity->field_data_test[0]->entity->id(), $referenced_entity->id()); + $this->entities[] = $entity; + + $entity = EntityTestMul::create(); + $entity->field_data_test->target_id = $referenced_entity->id(); + $entity->save(); + $this->assertEqual($entity->field_data_test[0]->entity->id(), $referenced_entity->id()); + $this->entities[] = $entity; + + Views::viewsData()->clear(); + + // Check the generated views data. + $views_data = Views::viewsData()->get('entity_test_mul__field_data_test'); + $this->assertEqual($views_data['field_data_test']['relationship']['id'], 'standard'); + $this->assertEqual($views_data['field_data_test']['relationship']['base'], 'entity_test'); + $this->assertEqual($views_data['field_data_test']['relationship']['base field'], 'id'); + $this->assertEqual($views_data['field_data_test']['relationship']['relationship field'], 'field_data_test_target_id'); + $this->assertEqual($views_data['field_data_test']['relationship']['entity type'], 'entity_test'); + + // Check the backwards reference. + $views_data = Views::viewsData()->get('entity_test'); + $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['id'], 'entity_reverse'); + $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['base'], 'entity_test_mul_property_data'); + $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['base field'], 'id'); + $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['field table'], 'entity_test_mul__field_data_test'); + $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['field field'], 'field_data_test_target_id'); + $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['field_name'], 'field_data_test'); + $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['entity_type'], 'entity_test_mul'); + $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['join_extra'][0], ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE]); + + // Check an actual test view. + $view = Views::getView('test_entity_reference_entity_test_mul_view'); + $this->executeView($view); + /** @var \Drupal\views\ResultRow $row */ + foreach ($view->result as $index => $row) { + // Check that the actual ID of the entity is the expected one. + $this->assertEqual($row->id, $this->entities[$index]->id()); + + // Also check that we have the correct result entity. + $this->assertEqual($row->_entity->id(), $this->entities[$index]->id()); + + // Test the forward relationship. + $this->assertEqual($row->entity_test_entity_test_mul__field_data_test_id, 1); + + // Test that the correct relationship entity is on the row. + $this->assertEqual($row->_relationship_entities['field_data_test']->id(), 1); + $this->assertEqual($row->_relationship_entities['field_data_test']->bundle(), 'entity_test'); + + } + + // Check the backwards reference view. + $view = Views::getView('test_entity_reference_reverse_entity_test_mul_view'); + $this->executeView($view); + /** @var \Drupal\views\ResultRow $row */ + foreach ($view->result as $index => $row) { + $this->assertEqual($row->id, 1); + $this->assertEqual($row->_entity->id(), 1); + + // Test the backwards relationship. + $this->assertEqual($row->field_data_test_entity_test_id, $this->entities[$index]->id()); + + // Test that the correct relationship entity is on the row. + $this->assertEqual($row->_relationship_entities['reverse__entity_test_mul__field_data_test']->id(), $this->entities[$index]->id()); + $this->assertEqual($row->_relationship_entities['reverse__entity_test_mul__field_data_test']->bundle(), 'entity_test_mul'); } } diff --git a/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_entity_test_mul_view.yml b/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_entity_test_mul_view.yml new file mode 100644 index 000000000000..8f8130242a40 --- /dev/null +++ b/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_entity_test_mul_view.yml @@ -0,0 +1,120 @@ +langcode: en +status: true +dependencies: + module: + - entity_test +id: test_entity_reference_entity_test_mul_view +label: test_entity_reference_entity_test_mul_view +module: views +description: '' +tag: '' +base_table: entity_test_mul_property_data +base_field: id +core: 8.x +display: + default: + display_plugin: default + id: default + display_title: Master + position: 0 + display_options: + access: + type: none + options: { } + cache: + type: none + options: { } + query: + type: views_query + options: + disable_sql_rewrite: false + distinct: false + replica: false + query_comment: '' + query_tags: { } + exposed_form: + type: basic + options: + submit_button: Apply + reset_button: false + reset_button_label: Reset + exposed_sorts_label: 'Sort by' + expose_sort_order: true + sort_asc_label: Asc + sort_desc_label: Desc + pager: + type: full + options: + items_per_page: 10 + offset: 0 + id: 0 + total_pages: null + expose: + items_per_page: false + items_per_page_label: 'Items per page' + items_per_page_options: '5, 10, 25, 50' + items_per_page_options_all: false + items_per_page_options_all_label: '- All -' + offset: false + offset_label: Offset + tags: + previous: '‹ previous' + next: 'next ›' + first: '« first' + last: 'last »' + quantity: 9 + style: + type: default + options: + grouping: { } + row_class: '' + default_row_class: true + uses_fields: false + row: + type: fields + options: + inline: { } + separator: '' + hide_empty: false + default_field_elements: true + fields: + id: + id: id + table: entity_test_mul_property_data + field: id + entity_type: entity_test_mul + entity_field: id + plugin_id: field + id_1: + id: id_1 + table: entity_test + field: id + entity_type: entity_test + entity_field: id + plugin_id: field + relationship: field_data_test + filters: { } + sorts: + id: + id: id + table: entity_test_mul_property_data + field: id + entity_type: entity_test_mul + entity_field: id + plugin_id: standard + header: { } + footer: { } + empty: { } + relationships: + field_data_test: + id: field_data_test + table: entity_test_mul__field_data_test + field: field_data_test + plugin_id: standard + arguments: { } + display_extenders: { } + cache_metadata: + contexts: + - languages + - 'languages:language_interface' + cacheable: false diff --git a/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_entity_test_view.yml b/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_entity_test_view.yml new file mode 100644 index 000000000000..bb381c21b22a --- /dev/null +++ b/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_entity_test_view.yml @@ -0,0 +1,121 @@ +langcode: en +status: true +dependencies: + module: + - entity_test +id: test_entity_reference_entity_test_view +label: test_entity_reference_entity_test_view +module: views +description: '' +tag: '' +base_table: entity_test +base_field: id +core: 8.x +display: + default: + display_plugin: default + id: default + display_title: Master + position: 0 + display_options: + access: + type: none + options: { } + cache: + type: none + options: { } + query: + type: views_query + options: + disable_sql_rewrite: false + distinct: false + replica: false + query_comment: '' + query_tags: { } + exposed_form: + type: basic + options: + submit_button: Apply + reset_button: false + reset_button_label: Reset + exposed_sorts_label: 'Sort by' + expose_sort_order: true + sort_asc_label: Asc + sort_desc_label: Desc + pager: + type: full + options: + items_per_page: 10 + offset: 0 + id: 0 + total_pages: null + expose: + items_per_page: false + items_per_page_label: 'Items per page' + items_per_page_options: '5, 10, 25, 50' + items_per_page_options_all: false + items_per_page_options_all_label: '- All -' + offset: false + offset_label: Offset + tags: + previous: '‹ previous' + next: 'next ›' + first: '« first' + last: 'last »' + quantity: 9 + style: + type: default + options: + grouping: { } + row_class: '' + default_row_class: true + uses_fields: false + row: + type: fields + options: + inline: { } + separator: '' + hide_empty: false + default_field_elements: true + fields: + id: + id: id + table: entity_test + field: id + entity_type: entity_test + entity_field: id + plugin_id: field + id_1: + id: id_1 + table: entity_test_mul + field: id + entity_type: entity_test_mul + entity_field: id + plugin_id: field + relationship: field_test_data + filters: { } + sorts: + id: + id: id + table: entity_test + field: id + entity_type: entity_test + entity_field: id + plugin_id: standard + header: { } + footer: { } + empty: { } + relationships: + field_test_data: + id: field_test_data + table: entity_test__field_test_data + field: field_test_data + plugin_id: standard + arguments: { } + display_extenders: { } + cache_metadata: + contexts: + - entity_test_view_grants + - languages + - 'languages:language_interface' + cacheable: false diff --git a/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_reverse_entity_test_mul_view.yml b/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_reverse_entity_test_mul_view.yml new file mode 100644 index 000000000000..8ee78f1e0d52 --- /dev/null +++ b/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_reverse_entity_test_mul_view.yml @@ -0,0 +1,122 @@ +langcode: en +status: true +dependencies: + module: + - entity_test +id: test_entity_reference_reverse_entity_test_mul_view +label: test_entity_reference_reverse_entity_test_mul_view +module: views +description: '' +tag: '' +base_table: entity_test +base_field: id +core: 8.x +display: + default: + display_plugin: default + id: default + display_title: Master + position: 0 + display_options: + access: + type: none + options: { } + cache: + type: none + options: { } + query: + type: views_query + options: + disable_sql_rewrite: false + distinct: false + replica: false + query_comment: '' + query_tags: { } + exposed_form: + type: basic + options: + submit_button: Apply + reset_button: false + reset_button_label: Reset + exposed_sorts_label: 'Sort by' + expose_sort_order: true + sort_asc_label: Asc + sort_desc_label: Desc + pager: + type: full + options: + items_per_page: 10 + offset: 0 + id: 0 + total_pages: null + expose: + items_per_page: false + items_per_page_label: 'Items per page' + items_per_page_options: '5, 10, 25, 50' + items_per_page_options_all: false + items_per_page_options_all_label: '- All -' + offset: false + offset_label: Offset + tags: + previous: '‹ previous' + next: 'next ›' + first: '« first' + last: 'last »' + quantity: 9 + style: + type: default + options: + grouping: { } + row_class: '' + default_row_class: true + uses_fields: false + row: + type: fields + options: + inline: { } + separator: '' + hide_empty: false + default_field_elements: true + fields: + id: + id: id + table: entity_test + field: id + entity_type: entity_test + entity_field: id + plugin_id: field + id_1: + id: id_1 + table: entity_test_mul_property_data + field: id + entity_type: entity_test_mul + entity_field: id + plugin_id: field + relationship: reverse__entity_test_mul__field_data_test + filters: { } + sorts: + id: + id: id + table: entity_test + field: id + entity_type: entity_test + entity_field: id + plugin_id: standard + header: { } + footer: { } + empty: { } + relationships: + reverse__entity_test_mul__field_data_test: + id: reverse__entity_test_mul__field_data_test + table: entity_test + field: reverse__entity_test_mul__field_data_test + entity_type: entity_test + plugin_id: entity_reverse + arguments: { } + display_extenders: { } + cache_metadata: + contexts: + - entity_test_view_grants + - languages + - 'languages:language_interface' + cacheable: false diff --git a/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_reverse_entity_test_view.yml b/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_reverse_entity_test_view.yml new file mode 100644 index 000000000000..e0c2ee2a9728 --- /dev/null +++ b/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_reverse_entity_test_view.yml @@ -0,0 +1,121 @@ +langcode: en +status: true +dependencies: + module: + - entity_test +id: test_entity_reference_reverse_entity_test_view +label: test_entity_reference_reverse_entity_test_view +module: views +description: '' +tag: '' +base_table: entity_test_mul_property_data +base_field: id +core: 8.x +display: + default: + display_plugin: default + id: default + display_title: Master + position: 0 + display_options: + access: + type: none + options: { } + cache: + type: none + options: { } + query: + type: views_query + options: + disable_sql_rewrite: false + distinct: false + replica: false + query_comment: '' + query_tags: { } + exposed_form: + type: basic + options: + submit_button: Apply + reset_button: false + reset_button_label: Reset + exposed_sorts_label: 'Sort by' + expose_sort_order: true + sort_asc_label: Asc + sort_desc_label: Desc + pager: + type: full + options: + items_per_page: 10 + offset: 0 + id: 0 + total_pages: null + expose: + items_per_page: false + items_per_page_label: 'Items per page' + items_per_page_options: '5, 10, 25, 50' + items_per_page_options_all: false + items_per_page_options_all_label: '- All -' + offset: false + offset_label: Offset + tags: + previous: '‹ previous' + next: 'next ›' + first: '« first' + last: 'last »' + quantity: 9 + style: + type: default + options: + grouping: { } + row_class: '' + default_row_class: true + uses_fields: false + row: + type: fields + options: + inline: { } + separator: '' + hide_empty: false + default_field_elements: true + fields: + id: + id: id + table: entity_test_mul_property_data + field: id + entity_type: entity_test_mul + entity_field: id + plugin_id: field + id_1: + id: id_1 + table: entity_test + field: id + entity_type: entity_test + entity_field: id + plugin_id: field + relationship: reverse__entity_test__field_test_data + filters: { } + sorts: + id: + id: id + table: entity_test_mul_property_data + field: id + entity_type: entity_test_mul + entity_field: id + plugin_id: standard + header: { } + footer: { } + empty: { } + relationships: + reverse__entity_test__field_test_data: + id: reverse__entity_test__field_test_data + table: entity_test_mul_property_data + field: reverse__entity_test__field_test_data + entity_type: entity_test_mul + plugin_id: entity_reverse + arguments: { } + display_extenders: { } + cache_metadata: + contexts: + - languages + - 'languages:language_interface' + cacheable: false diff --git a/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_view.yml b/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_view.yml deleted file mode 100644 index 90e4d31f333f..000000000000 --- a/core/modules/entity_reference/tests/modules/entity_reference_test_views/test_views/views.view.test_entity_reference_view.yml +++ /dev/null @@ -1,76 +0,0 @@ -langcode: en -status: true -dependencies: { } -id: test_entity_reference_view -label: null -module: views -description: '' -tag: '' -base_table: entity_test -base_field: nid -core: '8' -display: - default: - display_options: - defaults: - fields: false - relationships: false - pager: false - sorts: false - fields: - id: - field: id - id: id - relationship: none - table: entity_test - plugin_id: field - entity_type: entity_test - entity_field: id - id_1: - field: id - id: id_1 - relationship: test_relationship - table: entity_test - plugin_id: field - entity_type: entity_test - entity_field: id - pager: - options: - offset: 0 - type: none - sorts: - id: - field: id - id: id - order: ASC - relationship: none - table: entity_test - plugin_id: standard - entity_type: entity_test - entity_field: id - relationships: - test_relationship: - id: field_test - table: entity_test__field_test - field: field_test - relationship: none - plugin_id: standard - display_plugin: default - display_title: Master - id: default - position: 0 - embed_1: - display_options: - defaults: - relationships: false - relationships: - test_relationship: - id: reverse_field_test - table: entity_test - field: reverse__entity_test__field_test - relationship: none - plugin_id: standard - display_plugin: embed - display_title: Embed - id: embed_1 - position: null diff --git a/core/modules/file/file.views.inc b/core/modules/file/file.views.inc index 870a76a9b705..752513a6091f 100644 --- a/core/modules/file/file.views.inc +++ b/core/modules/file/file.views.inc @@ -49,15 +49,16 @@ function file_field_views_data_views_data_alter(array &$data, FieldStorageConfig $data['file_managed'][$pseudo_field_name]['relationship'] = array( 'title' => t('@entity using @field', array('@entity' => $entity_type->getLabel(), '@field' => $label)), + 'label' => t('@field_name', array('@field_name' => $field_name)), + 'group' => $entity_type->getLabel(), 'help' => t('Relate each @entity with a @field set to the file.', array('@entity' => $entity_type->getLabel(), '@field' => $label)), 'id' => 'entity_reverse', - 'field_name' => $field_name, + 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(), 'entity_type' => $entity_type_id, + 'base field' => $entity_type->getKey('id'), + 'field_name' => $field_name, 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), 'field field' => $field_name . '_target_id', - 'base' => $entity_type->getBaseTable(), - 'base field' => $entity_type->getKey('id'), - 'label' => t('!field_name', array('!field_name' => $field_name)), 'join_extra' => array( 0 => array( 'field' => 'deleted', diff --git a/core/modules/file/src/Tests/Views/FileViewsDataTest.php b/core/modules/file/src/Tests/Views/FileViewsDataTest.php new file mode 100644 index 000000000000..9ba1a644444e --- /dev/null +++ b/core/modules/file/src/Tests/Views/FileViewsDataTest.php @@ -0,0 +1,97 @@ + 'entity_test', + 'field_name' => 'field_base_file', + 'type' => 'file', + ))->save(); + FieldConfig::create(array( + 'entity_type' => 'entity_test', + 'field_name' => 'field_base_file', + 'bundle' => 'entity_test', + ))->save(); + // Check the generated views data. + $views_data = Views::viewsData()->get('entity_test__field_base_file'); + $relationship = $views_data['field_base_file_target_id']['relationship']; + $this->assertEqual($relationship['id'], 'standard'); + $this->assertEqual($relationship['base'], 'file_managed'); + $this->assertEqual($relationship['base field'], 'fid'); + $this->assertEqual($relationship['entity type'], 'file'); + // Check the backwards reference. + $views_data = Views::viewsData()->get('file_managed'); + $relationship = $views_data['reverse_field_base_file_entity_test']['relationship']; + $this->assertEqual($relationship['id'], 'entity_reverse'); + $this->assertEqual($relationship['base'], 'entity_test'); + $this->assertEqual($relationship['base field'], 'id'); + $this->assertEqual($relationship['field table'], 'entity_test__field_base_file'); + $this->assertEqual($relationship['field field'], 'field_base_file_target_id'); + $this->assertEqual($relationship['field_name'], 'field_base_file'); + $this->assertEqual($relationship['entity_type'], 'entity_test'); + $this->assertEqual($relationship['join_extra'][0], ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE]); + + // Create file field to entity_test_mul. + FieldStorageConfig::create(array( + 'entity_type' => 'entity_test_mul', + 'field_name' => 'field_data_file', + 'type' => 'file', + ))->save(); + FieldConfig::create(array( + 'entity_type' => 'entity_test_mul', + 'field_name' => 'field_data_file', + 'bundle' => 'entity_test_mul', + ))->save(); + // Check the generated views data. + $views_data = Views::viewsData()->get('entity_test_mul__field_data_file'); + $relationship = $views_data['field_data_file_target_id']['relationship']; + $this->assertEqual($relationship['id'], 'standard'); + $this->assertEqual($relationship['base'], 'file_managed'); + $this->assertEqual($relationship['base field'], 'fid'); + $this->assertEqual($relationship['entity type'], 'file'); + // Check the backwards reference. + $views_data = Views::viewsData()->get('file_managed'); + $relationship = $views_data['reverse_field_data_file_entity_test_mul']['relationship']; + $this->assertEqual($relationship['id'], 'entity_reverse'); + $this->assertEqual($relationship['base'], 'entity_test_mul_property_data'); + $this->assertEqual($relationship['base field'], 'id'); + $this->assertEqual($relationship['field table'], 'entity_test_mul__field_data_file'); + $this->assertEqual($relationship['field field'], 'field_data_file_target_id'); + $this->assertEqual($relationship['field_name'], 'field_data_file'); + $this->assertEqual($relationship['entity_type'], 'entity_test_mul'); + $this->assertEqual($relationship['join_extra'][0], ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE]); + } + +} diff --git a/core/modules/image/image.views.inc b/core/modules/image/image.views.inc index 1131e8775805..b854ff2b8a31 100644 --- a/core/modules/image/image.views.inc +++ b/core/modules/image/image.views.inc @@ -22,6 +22,7 @@ function image_field_views_data(FieldStorageConfigInterface $field_storage) { $data[$table_name][$field_storage->getName() . '_target_id']['relationship'] = array( 'id' => 'standard', 'base' => 'file_managed', + 'entity type' => 'file', 'base field' => 'fid', 'label' => t('image from !field_name', array('!field_name' => $field_storage->getName())), ); @@ -48,15 +49,16 @@ function image_field_views_data_views_data_alter(array &$data, FieldStorageConfi $data['file_managed'][$pseudo_field_name]['relationship'] = array( 'title' => t('@entity using @field', array('@entity' => $entity_type->getLabel(), '@field' => $label)), + 'label' => t('@field_name', array('@field_name' => $field_name)), 'help' => t('Relate each @entity with a @field set to the image.', array('@entity' => $entity_type->getLabel(), '@field' => $label)), + 'group' => $entity_type->getLabel(), 'id' => 'entity_reverse', - 'field_name' => $field_name, + 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(), 'entity_type' => $entity_type_id, + 'base field' => $entity_type->getKey('id'), + 'field_name' => $field_name, 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), 'field field' => $field_name . '_target_id', - 'base' => $entity_type->getBaseTable(), - 'base field' => $entity_type->getKey('id'), - 'label' => t('!field_name', array('!field_name' => $field_name)), 'join_extra' => array( 0 => array( 'field' => 'deleted', diff --git a/core/modules/image/src/Tests/Views/ImageViewsDataTest.php b/core/modules/image/src/Tests/Views/ImageViewsDataTest.php new file mode 100644 index 000000000000..82dd5906a062 --- /dev/null +++ b/core/modules/image/src/Tests/Views/ImageViewsDataTest.php @@ -0,0 +1,97 @@ + 'entity_test', + 'field_name' => 'field_base_image', + 'type' => 'image', + ))->save(); + FieldConfig::create(array( + 'entity_type' => 'entity_test', + 'field_name' => 'field_base_image', + 'bundle' => 'entity_test', + ))->save(); + // Check the generated views data. + $views_data = Views::viewsData()->get('entity_test__field_base_image'); + $relationship = $views_data['field_base_image_target_id']['relationship']; + $this->assertEqual($relationship['id'], 'standard'); + $this->assertEqual($relationship['base'], 'file_managed'); + $this->assertEqual($relationship['base field'], 'fid'); + $this->assertEqual($relationship['entity type'], 'file'); + // Check the backwards reference. + $views_data = Views::viewsData()->get('file_managed'); + $relationship = $views_data['reverse_field_base_image_entity_test']['relationship']; + $this->assertEqual($relationship['id'], 'entity_reverse'); + $this->assertEqual($relationship['base'], 'entity_test'); + $this->assertEqual($relationship['base field'], 'id'); + $this->assertEqual($relationship['field table'], 'entity_test__field_base_image'); + $this->assertEqual($relationship['field field'], 'field_base_image_target_id'); + $this->assertEqual($relationship['field_name'], 'field_base_image'); + $this->assertEqual($relationship['entity_type'], 'entity_test'); + $this->assertEqual($relationship['join_extra'][0], ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE]); + + // Create image field to entity_test_mul. + FieldStorageConfig::create(array( + 'entity_type' => 'entity_test_mul', + 'field_name' => 'field_data_image', + 'type' => 'image', + ))->save(); + FieldConfig::create(array( + 'entity_type' => 'entity_test_mul', + 'field_name' => 'field_data_image', + 'bundle' => 'entity_test_mul', + ))->save(); + // Check the generated views data. + $views_data = Views::viewsData()->get('entity_test_mul__field_data_image'); + $relationship = $views_data['field_data_image_target_id']['relationship']; + $this->assertEqual($relationship['id'], 'standard'); + $this->assertEqual($relationship['base'], 'file_managed'); + $this->assertEqual($relationship['base field'], 'fid'); + $this->assertEqual($relationship['entity type'], 'file'); + // Check the backwards reference. + $views_data = Views::viewsData()->get('file_managed'); + $relationship = $views_data['reverse_field_data_image_entity_test_mul']['relationship']; + $this->assertEqual($relationship['id'], 'entity_reverse'); + $this->assertEqual($relationship['base'], 'entity_test_mul_property_data'); + $this->assertEqual($relationship['base field'], 'id'); + $this->assertEqual($relationship['field table'], 'entity_test_mul__field_data_image'); + $this->assertEqual($relationship['field field'], 'field_data_image_target_id'); + $this->assertEqual($relationship['field_name'], 'field_data_image'); + $this->assertEqual($relationship['entity_type'], 'entity_test_mul'); + $this->assertEqual($relationship['join_extra'][0], ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE]); + } + +} diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulDefaultValue.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulDefaultValue.php index 48b13e2d8383..4bee585a9fe7 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulDefaultValue.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulDefaultValue.php @@ -26,8 +26,8 @@ use Drupal\Core\Field\BaseFieldDefinition; * "translation" = "Drupal\content_translation\ContentTranslationHandler", * "views_data" = "Drupal\views\EntityViewsData" * }, - * base_table = "entity_test_mul", - * data_table = "entity_test_mul_property_data", + * base_table = "entity_test_mul_default_value", + * data_table = "entity_test_mul_default_value_property_data", * translatable = TRUE, * entity_keys = { * "id" = "id",