diff --git a/src/Plugin/migrate/process/EntityLookup.php b/src/Plugin/migrate/process/EntityLookup.php index 2d120ea..2623d12 100644 --- a/src/Plugin/migrate/process/EntityLookup.php +++ b/src/Plugin/migrate/process/EntityLookup.php @@ -21,7 +21,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * This plugin looks for existing entities. * * @MigrateProcessPlugin( - * id = "entity_lookup" + * id = "entity_lookup", + * handle_multiples = TRUE * ) * * In its most simple form, this plugin needs no configuration. However, if the @@ -82,6 +83,9 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn /** @var string */ protected $lookupEntityType; + /** @var string */ + protected $destinationProperty; + /** * {@inheritdoc} */ @@ -115,6 +119,8 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) { $this->determineLookupProperties($destinationProperty); + $this->destinationProperty = $this->configuration['destination_field']; + return $this->query($value); } @@ -196,9 +202,12 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn // outcome. $ignoreCase = !empty($this->configuration['ignore_case']) ?: FALSE; + $multiple = is_array($value); + $query = $this->entityManager->getStorage($this->lookupEntityType) ->getQuery() - ->condition($this->lookupValueKey, $value); + ->condition($this->lookupValueKey, $value, $multiple ? 'IN' : NULL); + if ($this->lookupBundleKey) { $query->condition($this->lookupBundleKey, $this->lookupBundle); } @@ -208,6 +217,14 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn return NULL; } + if ($multiple && !empty($this->destinationProperty)) { + array_walk($results, function (&$value) { + $value = [$this->destinationProperty => $value]; + }); + + return array_values($results); + } + // By default do a case-sensitive comparison. if (!$ignoreCase) { // Returns the entity's identifier.