diff --git a/src/Plugin/migrate/process/EntityGenerate.php b/src/Plugin/migrate/process/EntityGenerate.php new file mode 100644 index 0000000..cd7898b --- /dev/null +++ b/src/Plugin/migrate/process/EntityGenerate.php @@ -0,0 +1,150 @@ +migration = $migration; + $this->entityManager = $entity_manager; + $this->setEntityType(); + $this->setValueKey(); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $migration, + $container->get('entity.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + if (!isset($this->configuration['entity_type']) || !is_string($this->configuration['entity_type'])) { + throw new MigrateException(sprintf('The entity_lookup plugin requires a "entity_type" configuration setting which should be a string, such as "node". Configuration is: %s', var_export($this->configuration, TRUE))); + } + if (!isset($this->configuration['value_key']) || !is_string($this->configuration['value_type'])) { + throw new MigrateException(sprintf('The entity_lookup plugin requires a "value_type" configuration setting which should be a string that represents a entity property or field name to search on, such as "title". Configuration is: %s', var_export($this->configuration, TRUE))); + } + + $value_key = $this->configuration['value_key']; + $bundle_key = $bundle_value = NULL; + if (isset($this->configuration['bundle']) && is_array($this->configuration['bundle'])) { + list($bundle_key, $bundle) = each($this->configuration['bundle']); + } + + // Creates a stub entity if one doesn't exist. + if (!($result = $this->query($value_key, $value, $bundle_key, $bundle))) { + return $this->generateEntity($value); + } + + return $result; + } + + /** + * Sets the 'entity_type' plugin configuration. + */ + abstract protected function setEntityType(); + + + /** + * Sets the 'value_key' plugin configuration. + */ + abstract protected function setValueKey(); + + /** + * Checks for the existence of some value. + * + * @param $value_key + *The property name on the entity on query. + * + * @param $value + * The value to query. + * + * @param $bundle_key + * The value key of the bundle for the entity. + * + * @param $bundle + * The bundle value of the entity. + * + * @return mixed|bool + * Entity id if the queried entity exists. Otherwise FALSE. + */ + abstract protected function query($value_key, $value, $bundle_key, $bundle); + + /** + * Generates stub entity for a given value. + * + * @param string $value + * Value to use in creation of stub entity. + * + * @return int|string + * The entity id of the generated entity. + */ + protected function generateEntity($value) { + return $this->entityManager + ->getStorage($this->configuration['entity_type']) + ->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) { + $value_key = $this->configuration['value_key']; + $stub = array( + $value_key => $value, + ); + if (isset($this->configuration['bundle']) && is_array($this->configuration['bundle'])) { + list($bundle_key, $bundle) = each($this->configuration['bundle']); + $stub[$bundle_key] = $bundle; + } + + return $stub; + } + +}