3 namespace Drupal\migrate_plus\Plugin\migrate\process;
5 use Drupal\migrate\MigrateExecutableInterface;
6 use Drupal\migrate\Row;
9 * This plugin generates entities within the process plugin.
11 * @MigrateProcessPlugin(
12 * id = "entity_generate"
17 * All the configuration from the lookup plugin applies here. In its most
18 * simple form, this plugin needs no configuration. If there are fields on the
19 * generated entity that are required or need some default value, that can be
20 * provided via a default_values configuration option.
22 * Example usage with default_values configuration:
25 * plugin: 'entity:node'
28 * plugin: default_value
31 * plugin: entity_generate
34 * description: Default description
35 * field_long_description: Default long description
38 class EntityGenerate extends EntityLookup {
43 public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
44 // Creates an entity if the lookup determines it doesn't exist.
45 if (!($result = parent::transform($value, $migrateExecutable, $row, $destinationProperty))) {
46 $result = $this->generateEntity($value);
53 * Generates an entity for a given value.
55 * @param string $value
56 * Value to use in creation of the entity.
59 * The entity id of the generated entity.
61 protected function generateEntity($value) {
63 $entity = $this->entityManager
64 ->getStorage($this->lookupEntityType)
65 ->create($this->entity($value));
73 * Fabricate an entity.
75 * This is intended to be extended by implementing classes to provide for more
76 * dynamic default values, rather than just static ones.
79 * Primary value to use in creation of the entity.
84 protected function entity($value) {
85 $entity_values = [$this->lookupValueKey => $value];
87 if ($this->lookupBundleKey) {
88 $entity_values[$this->lookupBundleKey] = $this->lookupBundle;
91 // Gather any static default values for properties/fields.
92 if (isset($this->configuration['default_values']) && is_array($this->configuration['default_values'])) {
93 foreach ($this->configuration['default_values'] as $key => $value) {
94 $entity_values[$key] = $value;
98 return $entity_values;