Skip to content
Snippets Groups Projects
Commit 6cee9c69 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3396282 by alexpott, shagel: Fix getting participant information from...

Issue #3396282 by alexpott, shagel: Fix getting participant information from entity reference fields with a cardinality greater than 1
parent b041d233
No related branches found
Tags 2.0.0-beta7
1 merge request!29Fix multiple cardinality reference fields
Pipeline #38143 passed
......@@ -21,11 +21,14 @@ class EntityReferenceField extends FieldParticipantListBase {
* {@inheritdoc}
*/
public function getParticipants(EntityInterface $entity): array {
$participants = [];
$definition = $this->getPluginDefinition();
if (!$entity->get($definition['field_name'])->isEmpty()) {
return $this->doGetParticipants($entity->get($definition['field_name'])->entity);
foreach ($entity->get($definition['field_name']) as $reference) {
$participants = array_merge($participants, $this->doGetParticipants($reference->entity));
}
}
return [];
return $participants;
}
/**
......
......@@ -2,6 +2,8 @@
namespace Drupal\Tests\vgwort\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\user\Entity\User;
/**
......@@ -11,6 +13,33 @@ use Drupal\user\Entity\User;
*/
class ParticipantPluginTest extends VgWortKernelTestBase {
protected function setUp(): void {
parent::setUp();
// Create an entity reference field so we can reference multiple users.
$field_storage = FieldStorageConfig::create([
'field_name' => 'users',
'entity_type' => static::ENTITY_TYPE,
'translatable' => FALSE,
'settings' => [
'target_type' => 'user',
],
'type' => 'entity_reference',
'cardinality' => 4,
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => static::ENTITY_TYPE,
'bundle' => static::ENTITY_TYPE,
'settings' => [
'handler' => 'default',
'handler_settings' => [],
],
]);
$field->save();
_vgwort_add_entity_reference_to_participant_map(static::ENTITY_TYPE, 'users');
}
public function testParticipantListManager() {
$entity_type = 'entity_test';
$entity = $this->container->get('entity_type.manager')
......@@ -78,7 +107,7 @@ class ParticipantPluginTest extends VgWortKernelTestBase {
// Add an entity owner with no VG Wort info.
$user = User::create(['name' => 'test', 'status' => TRUE]);
$user->save();
$entity->user_id->target_id = $user->id();
$entity->users = [['target_id' => $user->id()]];
$entity->save();
$participants = $participant_manager->getParticipants($entity);
$this->assertCount(3, $participants);
......@@ -86,17 +115,21 @@ class ParticipantPluginTest extends VgWortKernelTestBase {
$this->assertSame($anna_digby, $participants[0]->jsonSerialize());
$this->assertSame($sarah_smith_translator, $participants[2]->jsonSerialize());
// Add VG Wort to user.
$user->vgwort_test = [
// Add VG Wort to a new user.
$user2 = User::create(['name' => 'simon_george', 'status' => TRUE]);
$user2->vgwort_test = [
'card_number' => '45325342',
'firstname' => 'Simon',
'surname' => 'George',
'agency_abbr' => '',
];
$user->save();
$user2->save();
$entity->users = [['target_id' => $user->id()], ['target_id' => $user2->id()]];
$entity->save();
$storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
$storage->resetCache();
$entity = $storage->load($entity->id());
$participants = $participant_manager->getParticipants($entity);
$this->assertCount(4, $participants);
$simon_george = [
......@@ -112,13 +145,13 @@ class ParticipantPluginTest extends VgWortKernelTestBase {
// Make the user and entity participant info mostly match apart from
// involvement.
$user->vgwort_test = [
$user2->vgwort_test = [
'card_number' => '8924354',
'firstname' => 'Sarah',
'surname' => 'Smith',
'agency_abbr' => '',
];
$user->save();
$user2->save();
$storage->resetCache();
$entity = $storage->load($entity->id());
$participants = $participant_manager->getParticipants($entity);
......@@ -136,13 +169,13 @@ class ParticipantPluginTest extends VgWortKernelTestBase {
// Make the user and entity participant info mostly match apart from
// involvement.
$user->vgwort_test = [
$user2->vgwort_test = [
'card_number' => '123123123',
'firstname' => 'Bob',
'surname' => 'Jones',
'agency_abbr' => '',
];
$user->save();
$user2->save();
$storage->resetCache();
$entity = $storage->load($entity->id());
$participants = $participant_manager->getParticipants($entity);
......@@ -162,6 +195,35 @@ class ParticipantPluginTest extends VgWortKernelTestBase {
$this->assertSame($anna_digby, $participants[0]->jsonSerialize());
$this->assertSame(['code' => 'BBC', 'involvement' => 'AUTHOR'], $participants[3]->jsonSerialize());
$this->assertSame($sarah_smith_translator, $participants[2]->jsonSerialize());
// Make the first user in the users field have participant info.
$user->vgwort_test = [
'card_number' => '245243526',
'firstname' => 'Sam',
'surname' => 'Wilde',
'agency_abbr' => '',
];
$user->save();
$storage->resetCache();
$entity = $storage->load($entity->id());
$participants = $participant_manager->getParticipants($entity);
$this->assertCount(5, $participants);
$this->assertSame($bob_jones, $participants[1]->jsonSerialize());
$this->assertSame($anna_digby, $participants[0]->jsonSerialize());
$this->assertSame(['code' => 'BBC', 'involvement' => 'AUTHOR'], $participants[4]->jsonSerialize());
$this->assertSame($sarah_smith_translator, $participants[2]->jsonSerialize());
$this->assertSame([
'cardNumber' => 245243526,
'firstName' => 'Sam',
'surName' => 'Wilde',
'involvement' => 'AUTHOR',
], $participants[3]->jsonSerialize());
// Duplicate references to the same user.
$entity->user_id->target_id = $user->id();
$entity->save();
$participants = $participant_manager->getParticipants($entity);
$this->assertCount(5, $participants);
}
}
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