Commit b58ab8ce authored by alexpott's avatar alexpott

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,
),
),
);
}
......
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
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
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
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
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