Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate / process / EntityGenerate.php
1 <?php
2
3 namespace Drupal\migrate_plus\Plugin\migrate\process;
4
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;
12
13 /**
14  * This plugin generates entities within the process plugin.
15  *
16  * @MigrateProcessPlugin(
17  *   id = "entity_generate"
18  * )
19  *
20  * @see EntityLookup
21  *
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.
26  *
27  * Example usage with values and default_values configuration:
28  * @code
29  * destination:
30  *   plugin: 'entity:node'
31  * process:
32  *   type:
33  *     plugin: default_value
34  *     default_value: page
35  *   foo: bar
36  *   field_tags:
37  *     plugin: entity_generate
38  *     source: tags
39  *     default_values:
40  *       description: Default description
41  *     values:
42  *       field_long_description: some_source_field
43  *       field_foo: '@foo'
44  * @endcode
45  */
46 class EntityGenerate extends EntityLookup {
47
48   /**
49    * The row from the source to process.
50    *
51    * @var \Drupal\migrate\Row
52    */
53   protected $row;
54
55   /**
56    * The MigrateExecutable instance.
57    *
58    * @var \Drupal\migrate\MigrateExecutable
59    */
60   protected $migrateExecutable;
61
62   /**
63    * The get process plugin instance.
64    *
65    * @var \Drupal\migrate\Plugin\migrate\process\Get
66    */
67   protected $getProcessPlugin;
68
69   /**
70    * EntityGenerate constructor.
71    *
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
79    *   The 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.
86    */
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']]);
91     }
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration = NULL) {
98     return new static(
99       $configuration,
100       $pluginId,
101       $pluginDefinition,
102       $migration,
103       $container->get('entity.manager'),
104       $container->get('plugin.manager.entity_reference_selection'),
105       $container->get('plugin.manager.migrate.process')
106     );
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
113     $this->row = $row;
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);
118     }
119
120     return $result;
121   }
122
123   /**
124    * Generates an entity for a given value.
125    *
126    * @param string $value
127    *   Value to use in creation of the entity.
128    *
129    * @return int|string
130    *   The entity id of the generated entity.
131    */
132   protected function generateEntity($value) {
133     if (!empty($value)) {
134       $entity = $this->entityManager
135         ->getStorage($this->lookupEntityType)
136         ->create($this->entity($value));
137       $entity->save();
138
139       return $entity->id();
140     }
141   }
142
143   /**
144    * Fabricate an entity.
145    *
146    * This is intended to be extended by implementing classes to provide for more
147    * dynamic default values, rather than just static ones.
148    *
149    * @param mixed $value
150    *   Primary value to use in creation of the entity.
151    *
152    * @return array
153    *   Entity value array.
154    */
155   protected function entity($value) {
156     $entity_values = [$this->lookupValueKey => $value];
157
158     if ($this->lookupBundleKey) {
159       $entity_values[$this->lookupBundleKey] = $this->lookupBundle;
160     }
161
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;
166       }
167     }
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;
173       }
174     }
175
176     return $entity_values;
177   }
178
179 }