Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* @file
* Contains Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate.
*/
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* This plugin looks for existing entities.
*
* @MigrateProcessPlugin(
* id = "entity_lookup",
* handle_multiples = TRUE
* )
*
* In its most simple form, this plugin needs no configuration. However, if the
* lookup properties cannot be determined through introspection, define them via
* configuration.
*
* Example usage with minimal configuration:
* @code
* destination:
* plugin: 'entity:node'
* process:
* type:
* plugin: default_value
* default_value: page
* field_tags:
* plugin: entity_lookup
* source: tags
* @endcode
*
* Example usage with full configuration:
* @code
* field_tags:
* plugin: entity_lookup
* source: tags
* value_key: name
* bundle_key: vid
* bundle: tags
* entity_type: taxonomy_term
* ignore_case: true
* @endcode
*/
class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/** @var \Drupal\Core\Entity\EntityManagerInterface */
protected $entityManager;
/** @var \Drupal\migrate\Plugin\MigrationInterface */
protected $migration;
/** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface */
protected $selectionPluginManager;
/** @var string */
protected $destinationEntityType;
/** @var string|bool */
protected $destinationBundleKey;
/** @var string */
protected $lookupValueKey;
/** @var string */
protected $lookupBundleKey;
/** @var string */
protected $lookupBundle;
/** @var string */
protected $lookupEntityType;
/** @var string */
protected $destinationProperty;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration, EntityManagerInterface $entityManager, SelectionPluginManagerInterface $selectionPluginManager) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->migration = $migration;
$this->entityManager = $entityManager;
$this->selectionPluginManager = $selectionPluginManager;
$pluginIdParts = explode(':', $this->migration->getDestinationPlugin()->getPluginId());
$this->destinationEntityType = empty($pluginIdParts[1]) ?: $pluginIdParts[1];
$this->destinationBundleKey = !$this->destinationEntityType ?: $this->entityManager->getDefinition($this->destinationEntityType)->getKey('bundle');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$pluginId,
$pluginDefinition,
$migration,
$container->get('entity.manager'),
$container->get('plugin.manager.entity_reference_selection')
);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
$this->determineLookupProperties($destinationProperty);
$this->destinationProperty = $this->configuration['destination_field'];
return $this->query($value);
}
/**
* Determine the lookup properties from config or target field configuration.
*
* @param string $destinationProperty
* The destination property currently worked on. This is only used together
* with the $row above.
*/
protected function determineLookupProperties($destinationProperty) {
if (!empty($this->configuration['value_key'])) {
$this->lookupValueKey = $this->configuration['value_key'];
}
if (!empty($this->configuration['bundle_key'])) {
$this->lookupBundleKey = $this->configuration['bundle_key'];
}
if (!empty($this->configuration['bundle'])) {
$this->lookupBundle = $this->configuration['bundle'];
}
if (!empty($this->configuration['entity_type'])) {
$this->lookupEntityType = $this->configuration['entity_type'];
}
if (empty($this->lookupValueKey) || empty($this->lookupBundleKey) || empty($this->lookupBundle) || empty($this->lookupEntityType)) {
// See if we can introspect the lookup properties from the destination field.
if (!empty($this->migration->getProcess()[$this->destinationBundleKey][0]['default_value'])) {
$destinationEntityBundle = $this->migration->getProcess()[$this->destinationBundleKey][0]['default_value'];
$fieldConfig = $this->entityManager->getFieldDefinitions($this->destinationEntityType, $destinationEntityBundle)[$destinationProperty]->getConfig($destinationEntityBundle);
if ($fieldConfig->getType() != 'entity_reference') {
throw new MigrateException('The entity_lookup plugin found no entity reference field.');
}
if (empty($this->lookupBundle)) {
$handlerSettings = $fieldConfig->getSetting('handler_settings');
$bundles = array_filter((array) $handlerSettings['target_bundles']);
if (count($bundles) == 1) {
$this->lookupBundle = reset($bundles);
}
// This was added in 8.1.x is not supported in 8.0.x.
elseif (!empty($handlerSettings['auto_create']) && !empty($handlerSettings['auto_create_bundle'])) {
$this->lookupBundle = reset($handlerSettings['auto_create_bundle']);
}
}
// Make an assumption that if the selection handler can target more than
// one type of entity that we will use the first entity type.
$this->lookupEntityType = $this->lookupEntityType ?: reset($this->selectionPluginManager->createInstance($fieldConfig->getSetting('handler'))->getPluginDefinition()['entity_types']);
$this->lookupValueKey = $this->lookupValueKey ?: $this->entityManager->getDefinition($this->lookupEntityType)->getKey('label');
$this->lookupBundleKey = $this->lookupBundleKey ?: $this->entityManager->getDefinition($this->lookupEntityType)->getKey('bundle');
}
}
// If there aren't enough lookup properties available by now, then bail.
if (empty($this->lookupValueKey)) {
throw new MigrateException('The entity_lookup plugin requires a value_key, none located.');
}
if (!empty($this->lookupBundleKey) && empty($this->lookupBundle)) {
throw new MigrateException('The entity_lookup plugin found no bundle but destination entity requires one.');
}
if (empty($this->lookupEntityType)) {
throw new MigrateException('The entity_lookup plugin requires a entity_type, none located.');
}
}
/**
* Checks for the existence of some value.
*
* @param $value
* The value to query.
*
* @return mixed|null
* Entity id if the queried entity exists. Otherwise NULL.
*/
protected function query($value) {
// Entity queries typically are case-insensitive. Therefore, we need to
// handle case sensitive filtering as a post-query step. By default, it
// filters case insensitive. Change to true if that is not the desired
// outcome.
$ignoreCase = !empty($this->configuration['ignore_case']) ?: FALSE;
$multiple = is_array($value);
$query = $this->entityManager->getStorage($this->lookupEntityType)
->getQuery()
->condition($this->lookupValueKey, $value, $multiple ? 'IN' : NULL);
if ($this->lookupBundleKey) {
$query->condition($this->lookupBundleKey, $this->lookupBundle);
}
$results = $query->execute();
if (empty($results)) {
return NULL;
}
if ($multiple && !empty($this->destinationProperty)) {
array_walk($results, function (&$value) {
$value = [$this->destinationProperty => $value];
});
return array_values($results);
}
// By default do a case-sensitive comparison.
if (!$ignoreCase) {
// Returns the entity's identifier.
foreach ($results as $identifier) {
if ($value === $this->entityManager->getStorage($this->lookupEntityType)->load($identifier)->{$this->lookupValueKey}->value) {
return $identifier;
}
}
}
return reset($results);
}
}