Commit b58ab8ce authored by alexpott's avatar alexpott
Browse files

Issue #2451789 by olli, jibran: Entity reference joins to the wrong base table in views

parent a23ebe23
......@@ -13,45 +13,59 @@
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,
),
),
);
}
......
......@@ -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 @@
*/
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 @@ protected function setUp() {
$this->installEntitySchema('user');
$this->installEntitySchema('entity_test');
$this->installEntitySchema('entity_test_mul');
// 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_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();
$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'));
}
/**
* Tests using the views relationship.
*/
public function testNoDataTableRelationship() {
// Create some test entities which link each other.
$entity_storage= \Drupal::entityManager()->getStorage('entity_test');
$referenced_entity = $entity_storage->create(array());
$referenced_entity = EntityTestMul::create();
$referenced_entity->save();
$this->entities[$referenced_entity->id()] = $referenced_entity;
$entity = $entity_storage->create(array());
$entity->field_test->target_id = $referenced_entity->id();
$entity = EntityTest::create();
$entity->field_test_data->target_id = $referenced_entity->id();
$entity->save();
$this->assertEqual($entity->field_test[0]->entity->id(), $referenced_entity->id());
$this->entities[$entity->id()] = $entity;
$this->assertEqual($entity->field_test_data[0]->entity->id(), $referenced_entity->id());
$this->entities[] = $entity;
$entity = $entity_storage->create(array());
$entity->field_test->target_id = $referenced_entity->id();
$entity = EntityTest::create();
$entity->field_test_data->target_id = $referenced_entity->id();
$entity->save();
$this->assertEqual($entity->field_test[0]->entity->id(), $referenced_entity->id());
$this->entities[$entity->id()] = $entity;
$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 = 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_entity_test_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_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');
}
// 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);
// 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 using the views relationship.
* Tests views data generated for relationship.
*
* @see entity_reference_field_views_data()
*/
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 testDataTableRelationship() {
// 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');
// 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_view');
$view = Views::getView('test_entity_reference_entity_test_mul_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);
if ($index > 0) {
// Test that the correct relationship entity is on the row.
$this->assertEqual($view->result[$index]->_relationship_entities['test_relationship']->id(), 1);
}
$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');
}
$view->destroy();
$this->executeView($view, 'embed_1');
// 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());
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 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');
}
}
......
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
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