Skip to content
Snippets Groups Projects
Commit 7df19da2 authored by Edouard Cunibil's avatar Edouard Cunibil Committed by Lucas Hedding
Browse files

Issue #3227250 by duaelfr, tostinni, heddn: Entity lookup can't find an entity...

Issue #3227250 by duaelfr, tostinni, heddn: Entity lookup can't find an entity when looking for a reference field
parent b273092d
No related branches found
No related tags found
1 merge request!75Issue #3227250: Entity lookup can't find an entity when looking for a reference field
Pipeline #344982 passed with warnings
......@@ -4,10 +4,10 @@ declare(strict_types = 1);
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
......@@ -264,7 +264,15 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
// Returns the entity's identifier.
foreach ($results as $k => $identifier) {
$entity = $this->entityTypeManager->getStorage($this->lookupEntityType)->load($identifier);
$result_value = $entity instanceof ConfigEntityInterface ? $entity->get($this->lookupValueKey) : $entity->get($this->lookupValueKey)->value;
$result_value = $entity->get($this->lookupValueKey);
// If the value is a non-empty field, extract its first value's main
// property (most of the time "value" but sometimes "target_id" or
// anything declared by the field item).
if ($result_value instanceof FieldItemList && !$result_value->isEmpty()) {
$property = $result_value->first()->mainPropertyName();
$result_value = $result_value->{$property};
}
if (($multiple && !in_array($result_value, $value, TRUE)) || (!$multiple && $result_value !== $value)) {
unset($results[$k]);
}
......
......@@ -102,6 +102,47 @@ final class EntityLookupTest extends KernelTestBase {
$this->assertNull($value);
}
/**
* Lookup an entity on an entity_reference field.
*
* @covers ::transform
*/
public function testLookupEntityOnEntityReferenceField(): void {
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration([
'id' => 'test',
'source' => [],
'process' => [],
'destination' => [
'plugin' => 'entity:node',
],
]);
// Create a user.
$known_user = $this->createUser([], 'lucuma');
// Create a node owned by this user.
$known_node = $this->createNode([
'title' => 'Node test',
'uid' => $known_user->id(),
]);
$configuration = [
'entity_type' => 'node',
'value_key' => 'uid',
];
$plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_lookup', $configuration, $migration);
$row = new Row();
// Check the known node is found.
$value = $plugin->transform($known_user->id(), $this->migrateExecutable, $row, 'nid');
$this->assertSame($known_node->id(), $value);
// Check an unknown node is not found.
$value = $plugin->transform('not-an-id', $this->migrateExecutable, $row, 'nid');
$this->assertNull($value);
}
/**
* Tests a lookup of config entity.
*/
......
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