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