Commit 445bc901 authored by catch's avatar catch

Issue #2477899 by dawehner, kevin.dutra, borisson_, bojanz, catch, amateescu:...

Issue #2477899 by dawehner, kevin.dutra, borisson_, bojanz, catch, amateescu: Multiple valued Base fields won't work in Views
parent 3f52754e
<?php
namespace Drupal\entity_test\Entity;
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Defines an entity type with a multivalue base field.
*
* @ContentEntityType(
* id = "entity_test_multivalue_basefield",
* label = @Translation("Entity Test with a multivalue base field"),
* base_table = "entity_test_multivalue_basefield",
* data_table = "entity_test_multivalue_basefield_field_data",
* handlers = {
* "views_data" = "Drupal\views\EntityViewsData",
* },
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type",
* "label" = "name",
* "langcode" = "langcode",
* }
* )
*/
class EntityTestMultiValueBasefield extends EntityTest {
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['name']->setCardinality(2);
return $fields;
}
}
......@@ -57,6 +57,13 @@ class EntityViewsData implements EntityHandlerInterface, EntityViewsDataInterfac
*/
protected $fieldStorageDefinitions;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Constructs an EntityViewsData object.
*
......@@ -112,6 +119,7 @@ public function getViewsData() {
$data = [];
$base_table = $this->entityType->getBaseTable() ?: $this->entityType->id();
$views_revision_base_table = NULL;
$revisionable = $this->entityType->isRevisionable();
$base_field = $this->entityType->getKey('id');
......@@ -235,6 +243,7 @@ public function getViewsData() {
// Load all typed data definitions of all fields. This should cover each of
// the entity base, revision, data tables.
$field_definitions = $this->entityManager->getBaseFieldDefinitions($this->entityType->id());
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
if ($table_mapping = $this->storage->getTableMapping($field_definitions)) {
// Fetch all fields that can appear in both the base table and the data
// table.
......@@ -257,6 +266,36 @@ public function getViewsData() {
$this->mapFieldDefinition($table, $field_name, $field_definitions[$field_name], $table_mapping, $data[$table]);
}
}
foreach ($field_definitions as $field_definition) {
if ($table_mapping->requiresDedicatedTableStorage($field_definition->getFieldStorageDefinition())) {
$table = $table_mapping->getDedicatedDataTableName($field_definition->getFieldStorageDefinition());
$data[$table]['table']['group'] = $this->entityType->getLabel();
$data[$table]['table']['provider'] = $this->entityType->getProvider();
$data[$table]['table']['join'][$views_base_table] = [
'left_field' => $base_field,
'field' => 'entity_id',
'extra' => [
['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
],
];
if ($revisionable) {
$revision_table = $table_mapping->getDedicatedRevisionTableName($field_definition->getFieldStorageDefinition());
$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']['join'][$views_revision_base_table] = [
'left_field' => $revision_field,
'field' => 'entity_id',
'extra' => [
['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
],
];
}
}
}
}
// Add the entity type key to each table generated.
......
langcode: en
status: true
dependencies:
module:
- entity_test
id: test_entity_multivalue_basefield
label: ''
module: views
description: ''
tag: ''
base_table: entity_test_multivalue_basefield
base_field: id
core: '8'
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
fields:
id:
id: id
table: entity_test_multivalue_basefield
field: nid
relationship: none
plugin_id: field
entity_type: entity_test_multivalue_basefield
entity_field: id
name:
id: name
table: entity_test_multivalue_basefield__name
field: name
plugin_id: field
entity_type: entity_test_multivalue_basefield
entity_field: name
defaults:
fields: false
filters: false
sorts:
id:
id: id
table: entity_test_multivalue_basefield
field: id
order: asc
<?php
namespace Drupal\Tests\views\Kernel\Entity;
use Drupal\entity_test\Entity\EntityTestMultiValueBasefield;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
/**
* Tests entity views with multivalue base fields.
*
* @group views
*/
class EntityViewsWithMultivalueBasefieldTest extends ViewsKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['entity_test'];
/**
* {@inheritdoc}
*/
public static $testViews = ['test_entity_multivalue_basefield'];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->installEntitySchema('entity_test_multivalue_basefield');
}
/**
* Tests entity views with multivalue base fields.
*/
public function testView() {
EntityTestMultiValueBasefield::create([
'name' => 'test',
])->save();
EntityTestMultiValueBasefield::create([
'name' => ['test2', 'test3'],
])->save();
$view = Views::getView('test_entity_multivalue_basefield');
$view->execute();
$this->assertIdenticalResultset($view, [
['name' => ['test']],
['name' => ['test2', 'test3']],
], ['name' => 'name']);
}
}
......@@ -360,6 +360,13 @@ function views_update_8005() {
// Empty update function to rebuild the views data.
}
/**
* Clear caches due to updated entity views data.
*/
function views_update_8100() {
// Empty update to cause a cache flush so that views data is rebuilt.
}
/**
* @} End of "addtogroup updates-8.1.0".
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment