Commit 6fb30444 authored by catch's avatar catch

Issue #2621504 by dawehner, mpdonadio, Lendude, jibran:...

Issue #2621504 by dawehner, mpdonadio, Lendude, jibran: \Drupal\views\Plugin\views\field\Field::getValue needs to support entity reference items
parent 45d0a032
......@@ -1030,15 +1030,27 @@ public function getValue(ResultRow $values, $field = NULL) {
$field_item_definition = $field_item_list->getFieldDefinition();
if ($field_item_definition->getFieldStorageDefinition()->getCardinality() == 1) {
return $field ? $field_item_list->$field : $field_item_list->value;
}
$values = [];
foreach ($field_item_list as $field_item) {
$values[] = $field ? $field_item->$field : $field_item->value;
/** @var \Drupal\Core\Field\FieldItemInterface $field_item */
if ($field) {
$values[] = $field_item->$field;
}
// Find the value using the main property of the field. If no main
// property is provided fall back to 'value'.
elseif ($main_property_name = $field_item->mainPropertyName()) {
$values[] = $field_item->{$main_property_name};
}
else {
$values[] = $field_item->value;
}
}
if ($field_item_definition->getFieldStorageDefinition()->getCardinality() == 1) {
return reset($values);
}
else {
return $values;
}
return $values;
}
}
......@@ -23,6 +23,13 @@ display:
plugin_id: field
entity_type: entity_test
entity_field: id
field_test_multiple:
id: field_test_multiple
table: entity_test__field_test_multiple
field: field_test_multiple
plugin_id: field
entity_type: entity_test
entity_field: field_test_multiple
field_test:
id: field_test
table: entity_test__field_test
......@@ -30,6 +37,13 @@ display:
plugin_id: field
entity_type: entity_test
entity_field: field_test
user_id:
id: user_id
table: entity_test
field: user_id
plugin_id: field
entity_type: entity_test
entity_field: user_id
style:
type: html_list
sorts:
......
......@@ -10,6 +10,7 @@
use Drupal\user\Entity\User;
use Drupal\views\Plugin\views\field\Field;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
/**
......@@ -62,12 +63,15 @@ class FieldFieldTest extends ViewsKernelTestBase {
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp();
// First setup the needed entity types before installing the views.
parent::setUp(FALSE);
$this->installEntitySchema('entity_test');
$this->installEntitySchema('user');
$this->installEntitySchema('entity_test');
$this->installEntitySchema('entity_test_rev');
ViewTestData::createTestViews(get_class($this), array('views_test_config'));
// Bypass any field access.
$this->adminUser = User::create(['name' => $this->randomString()]);
$this->adminUser->save();
......@@ -546,4 +550,60 @@ public function testMissingBundleFieldRender() {
$this->assertEqual('', $executable->getStyle()->getField(6, 'field_test'));
}
/**
* Tests \Drupal\views\Plugin\views\field\Field::getValue
*/
public function testGetValueMethod() {
$bundle = 'test_bundle';
entity_test_create_bundle($bundle);
$field_multiple = FieldConfig::create([
'field_name' => 'field_test_multiple',
'entity_type' => 'entity_test',
'bundle' => 'test_bundle',
]);
$field_multiple->save();
foreach ($this->entities as $entity) {
$entity->delete();
}
$this->entities = [];
$this->entities[] = $entity = EntityTest::create([
'type' => 'entity_test',
'name' => 'test name',
'user_id' => $this->testUsers[0]->id(),
]);
$entity->save();
$this->entities[] = $entity = EntityTest::create([
'type' => 'entity_test',
'name' => 'test name 2',
'user_id' => $this->testUsers[0]->id(),
]);
$entity->save();
$this->entities[] = $entity = EntityTest::create([
'type' => $bundle,
'name' => 'test name 3',
'user_id' => $this->testUsers[0]->id(),
'field_test_multiple' => [1, 2, 3],
]);
$entity->save();
$executable = Views::getView('test_field_field_test');
$executable->execute();
$field_normal = $executable->field['field_test'];
$field_entity_reference = $executable->field['user_id'];
$field_multi_cardinality = $executable->field['field_test_multiple'];
$this->assertEquals($this->entities[0]->field_test->value, $field_normal->getValue($executable->result[0]));
$this->assertEquals($this->entities[0]->user_id->target_id, $field_entity_reference->getValue($executable->result[0]));
$this->assertEquals($this->entities[1]->field_test->value, $field_normal->getValue($executable->result[1]));
$this->assertEquals($this->entities[1]->user_id->target_id, $field_entity_reference->getValue($executable->result[1]));
$this->assertEquals([], $field_multi_cardinality->getValue($executable->result[0]));
$this->assertEquals([], $field_multi_cardinality->getValue($executable->result[1]));
$this->assertEquals([1, 2, 3], $field_multi_cardinality->getValue($executable->result[2]));
}
}
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