Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Contains Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 namespace Drupal\migrate_plus\Plugin\migrate\process;
|
Chris@0
|
9
|
Chris@0
|
10 use Drupal\migrate\MigrateExecutableInterface;
|
Chris@0
|
11 use Drupal\migrate\Row;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * This plugin generates entity stubs.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @MigrateProcessPlugin(
|
Chris@0
|
17 * id = "entity_generate"
|
Chris@0
|
18 * )
|
Chris@0
|
19 *
|
Chris@0
|
20 * @see EntityLookup
|
Chris@0
|
21 *
|
Chris@0
|
22 * All the configuration from the lookup plugin applies here. In it's most
|
Chris@0
|
23 * simple form, this plugin needs no configuration. If there are fields on the
|
Chris@0
|
24 * stub entity that are required or need some default value, that can be
|
Chris@0
|
25 * provided via a default_values configuration option.
|
Chris@0
|
26 *
|
Chris@0
|
27 * Example usage with default_values configuration:
|
Chris@0
|
28 * @code
|
Chris@0
|
29 * destination:
|
Chris@0
|
30 * plugin: 'entity:node'
|
Chris@0
|
31 * process:
|
Chris@0
|
32 * type:
|
Chris@0
|
33 * plugin: default_value
|
Chris@0
|
34 * default_value: page
|
Chris@0
|
35 * field_tags:
|
Chris@0
|
36 * plugin: entity_generate
|
Chris@0
|
37 * source: tags
|
Chris@0
|
38 * default_values:
|
Chris@0
|
39 * description: Stub description
|
Chris@0
|
40 * field_long_description: Stub long description
|
Chris@0
|
41 * @endcode
|
Chris@0
|
42 */
|
Chris@0
|
43 class EntityGenerate extends EntityLookup {
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * {@inheritdoc}
|
Chris@0
|
47 */
|
Chris@0
|
48 public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
|
Chris@0
|
49 // Creates a stub entity if one doesn't exist.
|
Chris@0
|
50 if (!($result = parent::transform($value, $migrateExecutable, $row, $destinationProperty))) {
|
Chris@0
|
51 $result = $this->generateEntity($value);
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 return $result;
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Generates stub entity for a given value.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @param string $value
|
Chris@0
|
61 * Value to use in creation of stub entity.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @return int|string
|
Chris@0
|
64 * The entity id of the generated entity.
|
Chris@0
|
65 */
|
Chris@0
|
66 protected function generateEntity($value) {
|
Chris@0
|
67 if(!empty($value)) {
|
Chris@0
|
68 $entity = $this->entityManager
|
Chris@0
|
69 ->getStorage($this->lookupEntityType)
|
Chris@0
|
70 ->create($this->stub($value));
|
Chris@0
|
71 $entity->save();
|
Chris@0
|
72
|
Chris@0
|
73 return $entity->id();
|
Chris@0
|
74 }
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Fabricate a stub entity.
|
Chris@0
|
79 *
|
Chris@0
|
80 * This is intended to be extended by implementing classes to provide for more
|
Chris@0
|
81 * dynamic default values, rather than just static ones.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @param $value
|
Chris@0
|
84 * Value to use in creation of stub entity.
|
Chris@0
|
85 *
|
Chris@0
|
86 * @return array
|
Chris@0
|
87 * The stub entity.
|
Chris@0
|
88 */
|
Chris@0
|
89 protected function stub($value) {
|
Chris@0
|
90 $stub = [$this->lookupValueKey => $value];
|
Chris@0
|
91
|
Chris@0
|
92 if ($this->lookupBundleKey) {
|
Chris@0
|
93 $stub[$this->lookupBundleKey] = $this->lookupBundle;
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 // Gather any static default values for properties/fields.
|
Chris@0
|
97 if (isset($this->configuration['default_values']) && is_array($this->configuration['default_values'])) {
|
Chris@0
|
98 foreach ($this->configuration['default_values'] as $key => $value) {
|
Chris@0
|
99 $stub[$key] = $value;
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 return $stub;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 }
|