Skip to content
Snippets Groups Projects
Unverified Commit 7896891d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2978848 by claudiu.cristea, amateescu:...

Issue #2978848 by claudiu.cristea, amateescu: EntityReferenceFieldItemList::referencedEntities() doesn't work for computed fields
parent f283ab55
No related branches found
No related tags found
No related merge requests found
...@@ -24,7 +24,7 @@ public function getConstraints() { ...@@ -24,7 +24,7 @@ public function getConstraints() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function referencedEntities() { public function referencedEntities() {
if (empty($this->list)) { if ($this->isEmpty()) {
return []; return [];
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_test\Plugin\Field\ComputedReferenceTestFieldItemList;
use Drupal\entity_test\Plugin\Field\ComputedTestFieldItemList; use Drupal\entity_test\Plugin\Field\ComputedTestFieldItemList;
/** /**
...@@ -39,6 +40,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ...@@ -39,6 +40,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setComputed(TRUE) ->setComputed(TRUE)
->setClass(ComputedTestFieldItemList::class); ->setClass(ComputedTestFieldItemList::class);
$fields['computed_reference_field'] = BaseFieldDefinition::create('entity_reference')
->setLabel('Computed Reference Field Test')
->setComputed(TRUE)
->setSetting('target_type', 'entity_test')
->setClass(ComputedReferenceTestFieldItemList::class);
return $fields; return $fields;
} }
......
<?php
namespace Drupal\entity_test\Plugin\Field;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
/**
* A computed entity reference field item list.
*/
class ComputedReferenceTestFieldItemList extends EntityReferenceFieldItemList {
use ComputedItemListTrait;
/**
* Compute the list property from state.
*/
protected function computeValue() {
foreach (\Drupal::state()->get('entity_test_reference_computed_target_ids', []) as $delta => $id) {
$this->list[$delta] = $this->createItem($delta, $id);
}
}
}
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
use Drupal\Core\TypedData\ListInterface; use Drupal\Core\TypedData\ListInterface;
use Drupal\Core\TypedData\Type\StringInterface; use Drupal\Core\TypedData\Type\StringInterface;
use Drupal\Core\TypedData\TypedDataInterface; use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestComputedField; use Drupal\entity_test\Entity\EntityTestComputedField;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType; use Drupal\node\Entity\NodeType;
...@@ -870,6 +871,32 @@ public function testComputedFields() { ...@@ -870,6 +871,32 @@ public function testComputedFields() {
$this->assertFalse($computed_item_list1->equals($computed_item_list2)); $this->assertFalse($computed_item_list1->equals($computed_item_list2));
} }
/**
* Tests an entity reference computed field.
*/
public function testEntityReferenceComputedField() {
$this->installEntitySchema('entity_test_computed_field');
// Create 2 entities to be referenced.
$ref1 = EntityTest::create(['name' => 'foo', 'type' => 'bar']);
$ref1->save();
$ref2 = EntityTest::create(['name' => 'baz', 'type' => 'bar']);
$ref2->save();
\Drupal::state()->set('entity_test_reference_computed_target_ids', [$ref1->id(), $ref2->id()]);
$entity = EntityTestComputedField::create([]);
$entity->save();
/** @var \Drupal\entity_test\Plugin\Field\ComputedReferenceTestFieldItemList $field */
$field = $entity->get('computed_reference_field');
/** @var \Drupal\Core\Entity\EntityInterface[] $referenced_entities */
$referenced_entities = $field->referencedEntities();
// Check that ::referencedEntities() is working with computed fields.
$this->assertEquals($ref1->id(), $referenced_entities[0]->id());
$this->assertEquals($ref2->id(), $referenced_entities[1]->id());
}
/** /**
* Executes the computed properties tests for the given entity type. * Executes the computed properties tests for the given entity type.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment