From 7df19da2eed35955656cf466da5418d81e6cdc35 Mon Sep 17 00:00:00 2001
From: Edouard Cunibil <11996-DuaelFr@users.noreply.drupalcode.org>
Date: Wed, 20 Nov 2024 16:53:21 +0000
Subject: [PATCH] Issue #3227250 by duaelfr, tostinni, heddn: Entity lookup
 can't find an entity when looking for a reference field

---
 src/Plugin/migrate/process/EntityLookup.php   | 12 +++++-
 .../migrate/process/EntityLookupTest.php      | 41 +++++++++++++++++++
 2 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/src/Plugin/migrate/process/EntityLookup.php b/src/Plugin/migrate/process/EntityLookup.php
index ea62ed97..fcd03702 100644
--- a/src/Plugin/migrate/process/EntityLookup.php
+++ b/src/Plugin/migrate/process/EntityLookup.php
@@ -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]);
         }
diff --git a/tests/src/Kernel/Plugin/migrate/process/EntityLookupTest.php b/tests/src/Kernel/Plugin/migrate/process/EntityLookupTest.php
index dfa0491f..f4daee79 100644
--- a/tests/src/Kernel/Plugin/migrate/process/EntityLookupTest.php
+++ b/tests/src/Kernel/Plugin/migrate/process/EntityLookupTest.php
@@ -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.
    */
-- 
GitLab