diff --git a/src/Plugin/migrate/process/EntityGenerate.php b/src/Plugin/migrate/process/EntityGenerate.php index 7ccab0b..9b655fc 100644 --- a/src/Plugin/migrate/process/EntityGenerate.php +++ b/src/Plugin/migrate/process/EntityGenerate.php @@ -16,6 +16,24 @@ * @MigrateProcessPlugin( * id = "entity_generate" * ) + * + * @see EntityLookup + * + * All the configuration from the lookup plugin applies here. In it's most + * simple form, this plugin needs no configuration. If there are fields on the + * stub entity that are required or need some default value, that can be + * provided via a default_values configuration option. + * + * Example usage with default_values configuration: + * @code + * process: + * field_tags: + * plugin: entity_generate + * source: tags + * default_values: + * description: Stub description + * field_long_description: Stub long description + * @endcode */ class EntityGenerate extends EntityLookup { @@ -42,7 +60,7 @@ public function transform($value, MigrateExecutableInterface $migrateExecutable, */ protected function generateEntity($value) { return $this->entityManager - ->getStorage($this->destinationEntityType) + ->getStorage($this->lookupEntityType) ->create($this->stub($value)) ->save() ->id(); @@ -51,6 +69,9 @@ protected function generateEntity($value) { /** * Fabricate a stub entity. * + * This is intended to be extended by implementing classes to provide for more + * dynamic default values, rather than just static ones. + * * @param $value * Value to use in creation of stub entity. * @@ -58,10 +79,10 @@ protected function generateEntity($value) { * The stub entity. */ protected function stub($value) { - $stub = [$this->stubValueKey => $value]; + $stub = [$this->lookupValueKey => $value]; - if ($this->stubBundleKey) { - $stub[$this->stubBundleKey] = $this->stubBundle; + if ($this->lookupBundleKey) { + $stub[$this->lookupBundleKey] = $this->lookupBundle; } // Gather any static default values for properties/fields. diff --git a/src/Plugin/migrate/process/EntityLookup.php b/src/Plugin/migrate/process/EntityLookup.php index a6a81bb..ccece8b 100644 --- a/src/Plugin/migrate/process/EntityLookup.php +++ b/src/Plugin/migrate/process/EntityLookup.php @@ -18,11 +18,40 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * This plugin generates entity stubs. + * This plugin looks for existing entities. * * @MigrateProcessPlugin( * id = "entity_lookup" * ) + * + * In it's most simple form, this plugin needs no configuration. However, if the + * lookup properties cannot be determined through introspection, define them via + * configuration. + * + * Example usage with minimal configuration: + * @code + * destination: + * plugin: 'entity:node' + * process: + * type: + * plugin: default_value + * default_value: page + * field_tags: + * plugin: entity_lookup + * source: tags + * @endcode + * + * Example usage with full configuration: + * @code + * field_tags: + * plugin: entity_lookup + * source: tags + * value_key: name + * bundle_key: vid + * bundle: tags + * entity_type: taxonomy_term + * ignore_case: true + * @endcode */ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface { @@ -42,16 +71,16 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn protected $destinationBundleKey; /** @var string */ - protected $stubValueKey; + protected $lookupValueKey; /** @var string */ - protected $stubBundleKey; + protected $lookupBundleKey; /** @var string */ - protected $stubBundle; + protected $lookupBundle; /** @var string */ - protected $stubEntityType; + protected $lookupEntityType; /** * {@inheritdoc} @@ -83,34 +112,34 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) { - $this->collectStubProperties($destinationProperty); + $this->determineLookupProperties($destinationProperty); return $this->query($value); } /** - * Collect the stub properties from config or target field configuration. + * Determine the lookup properties from config or target field configuration. * * @param string $destinationProperty * The destination property currently worked on. This is only used together * with the $row above. */ - protected function collectStubProperties($destinationProperty) { + protected function determineLookupProperties($destinationProperty) { if (!empty($this->configuration['value_key'])) { - $this->stubValueKey = $this->configuration['value_key']; + $this->lookupValueKey = $this->configuration['value_key']; } if (!empty($this->configuration['bundle_key'])) { - $this->stubBundleKey = $this->configuration['bundle_key']; + $this->lookupBundleKey = $this->configuration['bundle_key']; } if (!empty($this->configuration['bundle'])) { - $this->stubBundle = $this->configuration['bundle']; + $this->lookupBundle = $this->configuration['bundle']; } if (!empty($this->configuration['entity_type'])) { - $this->stubEntityType = $this->configuration['entity_type']; + $this->lookupEntityType = $this->configuration['entity_type']; } - if (empty($this->stubValueKey) || empty($this->stubBundleKey) || $this->stubBundle || empty($this->stubEntityType)) { - // See if we can introspect the stub properties from the destination field. + if (empty($this->lookupValueKey) || empty($this->lookupBundleKey) || $this->lookupBundle || empty($this->lookupEntityType)) { + // See if we can introspect the lookup properties from the destination field. if (!empty($this->migration->getProcess()[$this->destinationBundleKey][0]['default_value'])) { $destinationEntityBundle = $this->migration->getProcess()[$this->destinationBundleKey][0]['default_value']; $fieldConfig = $this->entityManager->getFieldDefinitions($this->destinationEntityType, $destinationEntityBundle)[$destinationProperty]->getConfig($destinationEntityBundle); @@ -118,34 +147,34 @@ protected function collectStubProperties($destinationProperty) { throw new MigrateException('The entity_lookup plugin found no entity reference field.'); } - if (empty($this->stubBundle)) { + if (empty($this->lookupBundle)) { $handlerSettings = $fieldConfig->getSetting('handler_settings'); $bundles = array_filter((array) $handlerSettings['target_bundles']); if (count($bundles) == 1) { - $this->stubBundle = reset($bundles); + $this->lookupBundle = reset($bundles); } // This was added in 8.1.x is not supported in 8.0.x. elseif (!empty($handlerSettings['auto_create']) && !empty($handlerSettings['auto_create_bundle'])) { - $this->stubBundle = reset($handlerSettings['auto_create_bundle']); + $this->lookupBundle = reset($handlerSettings['auto_create_bundle']); } } // Make an assumption that if the selection handler can target more than // one type of entity that we will use the first entity type. - $this->stubEntityType = $this->stubEntityType ?: reset($this->selectionPluginManager->createInstance($fieldConfig->getSetting('handler'))->getPluginDefinition()['entity_types']); - $this->stubValueKey = $this->stubValueKey ?: $this->entityManager->getDefinition($this->stubEntityType)->getKey('label'); - $this->stubBundleKey = $this->stubBundleKey ?: $this->entityManager->getDefinition($this->stubEntityType)->getKey('bundle'); + $this->lookupEntityType = $this->lookupEntityType ?: reset($this->selectionPluginManager->createInstance($fieldConfig->getSetting('handler'))->getPluginDefinition()['entity_types']); + $this->lookupValueKey = $this->lookupValueKey ?: $this->entityManager->getDefinition($this->lookupEntityType)->getKey('label'); + $this->lookupBundleKey = $this->lookupBundleKey ?: $this->entityManager->getDefinition($this->lookupEntityType)->getKey('bundle'); } } - // If there aren't enough stub properties available by now, then bail. - if (empty($this->stubValueKey)) { + // If there aren't enough lookup properties available by now, then bail. + if (empty($this->lookupValueKey)) { throw new MigrateException('The entity_lookup plugin requires a value_key, none located.'); } - if (!empty($this->stubBundleKey) && empty($this->stubBundle)) { + if (!empty($this->lookupBundleKey) && empty($this->lookupBundle)) { throw new MigrateException('The entity_lookup plugin found no bundle but destination entity requires one.'); } - if (empty($this->stubValueKey)) { + if (empty($this->lookupEntityType)) { throw new MigrateException('The entity_lookup plugin requires a entity_type, none located.'); } } @@ -160,13 +189,17 @@ protected function collectStubProperties($destinationProperty) { * Entity id if the queried entity exists. Otherwise FALSE. */ protected function query($value) { + // Entity queries typically are case-insensitive. Therefore, we need to + // handle case sensitive filtering as a post-query step. By default, it + // filters case insensitive. Change to true if that is not the desired + // outcome. $ignoreCase = !empty($this->configuration['ignore_case']) ?: FALSE; - $query = $this->entityManager->getStorage($this->stubEntityType) + $query = $this->entityManager->getStorage($this->lookupEntityType) ->getQuery() - ->condition($this->stubValueKey, $value); - if ($this->stubBundleKey) { - $query->condition($this->stubBundleKey, $this->stubBundle); + ->condition($this->lookupValueKey, $value); + if ($this->lookupBundleKey) { + $query->condition($this->lookupBundleKey, $this->lookupBundle); } $results = $query->execute(); @@ -174,7 +207,7 @@ protected function query($value) { if (!$ignoreCase) { // Returns the entity's identifier. foreach ($results as $identifier) { - if ($value === $this->entityManager->getStorage($this->stubEntityType)->load($identifier)->{$this->stubValueKey}->value) { + if ($value === $this->entityManager->getStorage($this->lookupEntityType)->load($identifier)->{$this->lookupValueKey}->value) { return $identifier; } }