3 namespace Drupal\migrate_plus\Plugin\migrate\process;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\Plugin\MigratePluginManager;
9 use Drupal\migrate\Plugin\MigrationInterface;
10 use Drupal\migrate\Row;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * This plugin generates entities within the process plugin.
16 * @MigrateProcessPlugin(
17 * id = "entity_generate"
22 * All the configuration from the lookup plugin applies here. In its most
23 * simple form, this plugin needs no configuration. If there are fields on the
24 * generated entity that are required or need some value, their values can be
25 * provided via values and/or default_values configuration options.
27 * Example usage with values and default_values configuration:
30 * plugin: 'entity:node'
33 * plugin: default_value
37 * plugin: entity_generate
40 * description: Default description
42 * field_long_description: some_source_field
46 class EntityGenerate extends EntityLookup {
49 * The row from the source to process.
51 * @var \Drupal\migrate\Row
56 * The MigrateExecutable instance.
58 * @var \Drupal\migrate\MigrateExecutable
60 protected $migrateExecutable;
63 * The get process plugin instance.
65 * @var \Drupal\migrate\Plugin\migrate\process\Get
67 protected $getProcessPlugin;
70 * EntityGenerate constructor.
72 * @param array $configuration
73 * A configuration array containing information about the plugin instance.
74 * @param string $pluginId
75 * The plugin_id for the plugin instance.
76 * @param mixed $pluginDefinition
77 * The plugin implementation definition.
78 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
80 * @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
81 * The $entityManager instance.
82 * @param \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selectionPluginManager
83 * The $selectionPluginManager instance.
84 * @param \Drupal\migrate\Plugin\MigratePluginManager $migratePluginManager
85 * The MigratePluginManager instance.
87 public function __construct(array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration, EntityManagerInterface $entityManager, SelectionPluginManagerInterface $selectionPluginManager, MigratePluginManager $migratePluginManager) {
88 parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $entityManager, $selectionPluginManager);
89 if (isset($configuration['values'])) {
90 $this->getProcessPlugin = $migratePluginManager->createInstance('get', ['source' => $configuration['values']]);
97 public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration = NULL) {
103 $container->get('entity.manager'),
104 $container->get('plugin.manager.entity_reference_selection'),
105 $container->get('plugin.manager.migrate.process')
112 public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
114 $this->migrateExecutable = $migrateExecutable;
115 // Creates an entity if the lookup determines it doesn't exist.
116 if (!($result = parent::transform($value, $migrateExecutable, $row, $destinationProperty))) {
117 $result = $this->generateEntity($value);
124 * Generates an entity for a given value.
126 * @param string $value
127 * Value to use in creation of the entity.
130 * The entity id of the generated entity.
132 protected function generateEntity($value) {
133 if (!empty($value)) {
134 $entity = $this->entityManager
135 ->getStorage($this->lookupEntityType)
136 ->create($this->entity($value));
139 return $entity->id();
144 * Fabricate an entity.
146 * This is intended to be extended by implementing classes to provide for more
147 * dynamic default values, rather than just static ones.
149 * @param mixed $value
150 * Primary value to use in creation of the entity.
153 * Entity value array.
155 protected function entity($value) {
156 $entity_values = [$this->lookupValueKey => $value];
158 if ($this->lookupBundleKey) {
159 $entity_values[$this->lookupBundleKey] = $this->lookupBundle;
162 // Gather any static default values for properties/fields.
163 if (isset($this->configuration['default_values']) && is_array($this->configuration['default_values'])) {
164 foreach ($this->configuration['default_values'] as $key => $value) {
165 $entity_values[$key] = $value;
168 // Gather any additional properties/fields.
169 if (isset($this->configuration['values']) && is_array($this->configuration['values'])) {
170 foreach ($this->configuration['values'] as $key => $property) {
171 $source_value = $this->getProcessPlugin->transform(NULL, $this->migrateExecutable, $this->row, $property);
172 $entity_values[$key] = $source_value;
176 return $entity_values;