Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate / process / EntityGenerate.php
index 7361f732790cd7076f169bf85349cff11415a409..a749a6388cf42c80dd5939f8dd6c6e8fe2f6d749 100644 (file)
@@ -2,8 +2,13 @@
 
 namespace Drupal\migrate_plus\Plugin\migrate\process;
 
+use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
 use Drupal\migrate\MigrateExecutableInterface;
+use Drupal\migrate\Plugin\MigratePluginManager;
+use Drupal\migrate\Plugin\MigrationInterface;
 use Drupal\migrate\Row;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * This plugin generates entities within the process plugin.
@@ -16,10 +21,10 @@ use Drupal\migrate\Row;
  *
  * All the configuration from the lookup plugin applies here. In its most
  * simple form, this plugin needs no configuration. If there are fields on the
- * generated entity that are required or need some default value, that can be
- * provided via a default_values configuration option.
+ * generated entity that are required or need some value, their values can be
+ * provided via values and/or default_values configuration options.
  *
- * Example usage with default_values configuration:
+ * Example usage with values and default_values configuration:
  * @code
  * destination:
  *   plugin: 'entity:node'
@@ -27,20 +32,86 @@ use Drupal\migrate\Row;
  *   type:
  *     plugin: default_value
  *     default_value: page
+ *   foo: bar
  *   field_tags:
  *     plugin: entity_generate
  *     source: tags
  *     default_values:
  *       description: Default description
- *       field_long_description: Default long description
+ *     values:
+ *       field_long_description: some_source_field
+ *       field_foo: '@foo'
  * @endcode
  */
 class EntityGenerate extends EntityLookup {
 
+  /**
+   * The row from the source to process.
+   *
+   * @var \Drupal\migrate\Row
+   */
+  protected $row;
+
+  /**
+   * The MigrateExecutable instance.
+   *
+   * @var \Drupal\migrate\MigrateExecutable
+   */
+  protected $migrateExecutable;
+
+  /**
+   * The get process plugin instance.
+   *
+   * @var \Drupal\migrate\Plugin\migrate\process\Get
+   */
+  protected $getProcessPlugin;
+
+  /**
+   * EntityGenerate constructor.
+   *
+   * @param array $configuration
+   *   A configuration array containing information about the plugin instance.
+   * @param string $pluginId
+   *   The plugin_id for the plugin instance.
+   * @param mixed $pluginDefinition
+   *   The plugin implementation definition.
+   * @param \Drupal\migrate\Plugin\MigrationInterface $migration
+   *   The migration.
+   * @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
+   *   The $entityManager instance.
+   * @param \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selectionPluginManager
+   *   The $selectionPluginManager instance.
+   * @param \Drupal\migrate\Plugin\MigratePluginManager $migratePluginManager
+   *   The MigratePluginManager instance.
+   */
+  public function __construct(array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration, EntityManagerInterface $entityManager, SelectionPluginManagerInterface $selectionPluginManager, MigratePluginManager $migratePluginManager) {
+    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $entityManager, $selectionPluginManager);
+    if (isset($configuration['values'])) {
+      $this->getProcessPlugin = $migratePluginManager->createInstance('get', ['source' => $configuration['values']]);
+    }
+  }
+
+  /**
+   * {@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'),
+      $container->get('plugin.manager.entity_reference_selection'),
+      $container->get('plugin.manager.migrate.process')
+    );
+  }
+
   /**
    * {@inheritdoc}
    */
   public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
+    $this->row = $row;
+    $this->migrateExecutable = $migrateExecutable;
     // Creates an entity if the lookup determines it doesn't exist.
     if (!($result = parent::transform($value, $migrateExecutable, $row, $destinationProperty))) {
       $result = $this->generateEntity($value);
@@ -94,6 +165,13 @@ class EntityGenerate extends EntityLookup {
         $entity_values[$key] = $value;
       }
     }
+    // Gather any additional properties/fields.
+    if (isset($this->configuration['values']) && is_array($this->configuration['values'])) {
+      foreach ($this->configuration['values'] as $key => $property) {
+        $source_value = $this->getProcessPlugin->transform(NULL, $this->migrateExecutable, $this->row, $property);
+        $entity_values[$key] = $source_value;
+      }
+    }
 
     return $entity_values;
   }