diff --git a/src/Plugin/migrate/process/EntityGenerate.php b/src/Plugin/migrate/process/EntityGenerate.php new file mode 100644 index 0000000..b0f30b7 --- /dev/null +++ b/src/Plugin/migrate/process/EntityGenerate.php @@ -0,0 +1,185 @@ +migration = $migration; + $this->entityManager = $entityManager; + $this->entityType = substr($this->getPluginId(), 7); + $this->bundleKey = !empty($this->entityManager->getBundleInfo($this->entityType)['bundle']) ?: $this->entityManager->getBundleInfo($this->entityType)['bundle']; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration = NULL) { + return new static( + $configuration, + $pluginId, + $pluginDefinition, + $migration, + $container->get('entity.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) { + if (!empty($this->configuration['value_key'])) { + $this->valueKey = $this->configuration['value_key']; + } + elseif (!empty($valueKey) && !empty($this->entityManager->getBundleInfo($this->entityType)['label'])) { + $this->valueKey = $this->entityManager->getBundleInfo($this->entityType)['label']; + } + if (empty($this->valueKey)) { + throw new MigrateException('The entity_generate plugin requires a value_key, none located.'); + } + + if (!empty($this->configuration['bundle'])) { + $bundle = $this->configuration['bundle']; + } + elseif (!empty($bundle) && !empty($this->migration->getProcess()[$this->bundleKey]['default_value'])) { + $destinationEntityBundle = $this->migration->getProcess()[$this->bundleKey]['default_value']; + $fieldConfig = $this->entityManager->getFieldDefinitions($this->entityType, $destinationEntityBundle)[$destinationProperty]->getConfig($destinationEntityBundle); + if ('entity_reference' != $fieldConfig->getType()) { + throw new MigrateException('The entity_generate plugin found no entity reference field.'); + } + if ($fieldConfig->getSetting('auto_create')) { + $bundle = reset($fieldConfig->getSetting('auto_create_bundle')); + } + } + if ($this->bundleKey && empty($bundle)) { + throw new MigrateException('The entity_generate plugin found no bundle but destination entity requires one.'); + } + + // Creates a stub entity if one doesn't exist and stubbing isn't disabled. + if (!($result = $this->query($value, $bundle)) && empty($this->configuration['no_stub'])) { + return $this->generateEntity($value, $bundle); + } + + return $result ?: NULL; + } + + + /** + * Checks for the existence of some value. + * + * @param $value + * The value to query. + * + * @param $bundle + * The bundle value of the entity. + * + * @return mixed|bool + * Entity id if the queried entity exists. Otherwise FALSE. + */ + protected function query($value, $bundle) { + $ignoreCase = isset($this->configuration['ignore_case']) ?: FALSE; + + $query = $this->entityManager->getStorage($this->entityType) + ->getQuery() + ->condition($this->valueKey, $value); + if ($this->bundleKey) { + $query->condition($this->bundleKey, $bundle); + } + $results = $query->execute(); + + // By default do a case-sensitive comparison. + if (!$ignoreCase) { + // Returns the entity's identifier. + foreach ($this->entityManager->getStorage($this->entityType)->loadMultiple($results) as $identifier => $entity) { + if ($value === $entity->{$this->valueKey}->value()) { + return $identifier; + } + } + } + + return reset($results); + } + + /** + * Generates stub entity for a given value. + * + * @param string $value + * Value to use in creation of stub entity. + * @param string $bundle + * The bundle for the stub entity. + * + * @return int|string + * The entity id of the generated entity. + */ + protected function generateEntity($value, $bundle) { + return $this->entityManager + ->getStorage($this->entityType) + ->create($this->stub($value)) + ->save() + ->id(); + } + + /** + * Fabricate a stub entity. + * + * @param $value + * Value to use in creation of stub entity. + * + * @return array + * The stub entity. + */ + protected function stub($value, $bundle) { + $stub = array( + $this->valueKey => $value, + ); + if ($this->bundleKey) { + $stub[$this->bundleKey] = $bundle; + } + + return $stub; + } + +}