7361f732790cd7076f169bf85349cff11415a409
[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\migrate\MigrateExecutableInterface;
6 use Drupal\migrate\Row;
7
8 /**
9  * This plugin generates entities within the process plugin.
10  *
11  * @MigrateProcessPlugin(
12  *   id = "entity_generate"
13  * )
14  *
15  * @see EntityLookup
16  *
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.
21  *
22  * Example usage with default_values configuration:
23  * @code
24  * destination:
25  *   plugin: 'entity:node'
26  * process:
27  *   type:
28  *     plugin: default_value
29  *     default_value: page
30  *   field_tags:
31  *     plugin: entity_generate
32  *     source: tags
33  *     default_values:
34  *       description: Default description
35  *       field_long_description: Default long description
36  * @endcode
37  */
38 class EntityGenerate extends EntityLookup {
39
40   /**
41    * {@inheritdoc}
42    */
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);
47     }
48
49     return $result;
50   }
51
52   /**
53    * Generates an entity for a given value.
54    *
55    * @param string $value
56    *   Value to use in creation of the entity.
57    *
58    * @return int|string
59    *   The entity id of the generated entity.
60    */
61   protected function generateEntity($value) {
62     if (!empty($value)) {
63       $entity = $this->entityManager
64         ->getStorage($this->lookupEntityType)
65         ->create($this->entity($value));
66       $entity->save();
67
68       return $entity->id();
69     }
70   }
71
72   /**
73    * Fabricate an entity.
74    *
75    * This is intended to be extended by implementing classes to provide for more
76    * dynamic default values, rather than just static ones.
77    *
78    * @param mixed $value
79    *   Primary value to use in creation of the entity.
80    *
81    * @return array
82    *   Entity value array.
83    */
84   protected function entity($value) {
85     $entity_values = [$this->lookupValueKey => $value];
86
87     if ($this->lookupBundleKey) {
88       $entity_values[$this->lookupBundleKey] = $this->lookupBundle;
89     }
90
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;
95       }
96     }
97
98     return $entity_values;
99   }
100
101 }