|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Driver\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\PluginBase; |
|
6
|
|
|
use Drupal\Driver\Exception\Exception; |
|
7
|
|
|
use Drupal\Driver\Wrapper\Field\DriverFieldInterface; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
9
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
10
|
|
|
use Drupal\Core\Entity\EntityInterface; |
|
11
|
|
|
use Drupal\Driver\Wrapper\Field\DriverFieldDrupal8; |
|
12
|
|
|
use Drupal\Driver\Wrapper\Entity\DriverEntityInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Provides a base class for the Driver's entity plugins. |
|
16
|
|
|
*/ |
|
17
|
|
|
class DriverEntityPluginDrupal8Base extends DriverEntityPluginBase implements DriverEntityPluginInterface, DriverEntityInterface |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The id of the attached entity. |
|
22
|
|
|
* |
|
23
|
|
|
* @var integer|string; |
|
24
|
|
|
* |
|
25
|
|
|
* @deprecated Use id() instead. |
|
26
|
|
|
*/ |
|
27
|
|
|
public $id; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Entity type definition. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Drupal\Core\Entity\EntityStorageInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $storage; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The saved Drupal entity this object is wrapping for the Driver. |
|
38
|
|
|
* |
|
39
|
|
|
* @var \Drupal\Core\Entity\EntityInterface; |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $entity; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The driver field plugin manager. |
|
45
|
|
|
* |
|
46
|
|
|
* @var \Drupal\Driver\Plugin\DriverPluginManagerInterface; |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $fieldPluginManager; |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The drupal entity type manager. |
|
53
|
|
|
* |
|
54
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface; |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $entityTypeManager; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct( |
|
62
|
|
|
array $configuration, |
|
63
|
|
|
$plugin_id, |
|
64
|
|
|
$plugin_definition |
|
65
|
|
|
) { |
|
66
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
|
67
|
|
|
$this->entityTypeManager = \Drupal::entityTypeManager(); |
|
68
|
|
|
$this->storage = $this->entityTypeManager->getStorage($this->type); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function delete() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->getEntity()->delete(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getBundleKey() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->entityTypeManager |
|
85
|
|
|
->getDefinition($this->type) |
|
86
|
|
|
->getKey('bundle'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getBundleKeyLabels() |
|
93
|
|
|
{ |
|
94
|
|
|
$bundleKeyLabel = null; |
|
95
|
|
|
$bundleKey = $this->getBundleKey(); |
|
96
|
|
|
if (!empty($bundleKey)) { |
|
97
|
|
|
$definitions = \Drupal::service('entity_field.manager') |
|
98
|
|
|
->getBaseFieldDefinitions($this->type); |
|
99
|
|
|
$bundleKeyLabel = $definitions[$bundleKey]->getLabel(); |
|
100
|
|
|
} |
|
101
|
|
|
return [(string) $bundleKeyLabel]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getBundles() |
|
108
|
|
|
{ |
|
109
|
|
|
$bundleInfo = \Drupal::service('entity_type.bundle.info')->getBundleInfo($this->type); |
|
110
|
|
|
// Parse into array structure used by DriverNameMatcher. |
|
111
|
|
|
$bundles = []; |
|
112
|
|
|
foreach ($bundleInfo as $machineName => $bundleSettings) { |
|
113
|
|
|
$bundles[$bundleSettings['label']] = $machineName; |
|
114
|
|
|
} |
|
115
|
|
|
return $bundles; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getEntity() |
|
122
|
|
|
{ |
|
123
|
|
|
$entity = parent::getEntity(); |
|
124
|
|
|
if (!$entity instanceof EntityInterface) { |
|
|
|
|
|
|
125
|
|
|
throw new \Exception("Failed to obtain valid entity"); |
|
126
|
|
|
} |
|
127
|
|
|
return $this->entity; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getLabelKeys() |
|
134
|
|
|
{ |
|
135
|
|
|
$labelKeys = parent::getLabelKeys(); |
|
136
|
|
|
if (empty($labelKeys)) { |
|
137
|
|
|
$labelKeys = [ |
|
138
|
|
|
$this->entityTypeManager |
|
139
|
|
|
->getDefinition($this->type) |
|
140
|
|
|
->getKey('label') |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
return $labelKeys; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* {@inheritdoc} |
|
148
|
|
|
*/ |
|
149
|
|
|
public function id() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->getEntity()->id(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
|
|
public function isNew() |
|
158
|
|
|
{ |
|
159
|
|
|
if ($this->hasEntity() && !$this->entity->isNew()) { |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
return true; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritdoc} |
|
167
|
|
|
*/ |
|
168
|
|
|
public function label() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->getEntity()->label(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* {@inheritdoc} |
|
175
|
|
|
*/ |
|
176
|
|
|
public function load($entityId) |
|
177
|
|
|
{ |
|
178
|
|
|
if ($this->hasEntity()) { |
|
179
|
|
|
throw new \Exception("A Drupal entity is already attached to this plugin"); |
|
180
|
|
|
} |
|
181
|
|
|
$this->entity = $this->getStorage()->load($entityId); |
|
182
|
|
|
$this->id = is_null($this->entity) ? NULL : $this->id(); |
|
|
|
|
|
|
183
|
|
|
return $this->entity; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* {@inheritdoc} |
|
188
|
|
|
*/ |
|
189
|
|
|
public function reload() |
|
190
|
|
|
{ |
|
191
|
|
|
if (!$this->hasEntity()) { |
|
192
|
|
|
throw new \Exception("There is no attached entity so it cannot be reloaded"); |
|
193
|
|
|
} |
|
194
|
|
|
$entityId = $this->getEntity()->id(); |
|
195
|
|
|
$this->getStorage()->resetCache([$entityId]); |
|
196
|
|
|
$this->entity = $this->getStorage()->load($entityId); |
|
197
|
|
|
return $this->entity; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* {@inheritdoc} |
|
202
|
|
|
*/ |
|
203
|
|
|
public function save() |
|
204
|
|
|
{ |
|
205
|
|
|
$this->getEntity()->save(); |
|
206
|
|
|
$this->id = $this->id(); |
|
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* {@inheritdoc} |
|
211
|
|
|
*/ |
|
212
|
|
|
public function set($identifier, $field) |
|
213
|
|
|
{ |
|
214
|
|
|
if (!($field instanceof DriverFieldInterface)) { |
|
215
|
|
|
$field = $this->getNewDriverField($identifier, $field); |
|
216
|
|
|
} |
|
217
|
|
|
$this->getEntity()->set($field->getName(), $field->getProcessedValues()); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* {@inheritdoc} |
|
222
|
|
|
*/ |
|
223
|
|
|
public function url($rel = 'canonical', $options = []) |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->getEntity()->url($rel, $options); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get a new driver field with values. |
|
230
|
|
|
* |
|
231
|
|
|
* @param string $fieldName |
|
232
|
|
|
* A string identifying an entity field. |
|
233
|
|
|
* @param string|array $values |
|
234
|
|
|
* An input that can be transformed into Driver field values. |
|
235
|
|
|
*/ |
|
236
|
|
|
protected function getNewDriverField($fieldName, $values) |
|
237
|
|
|
{ |
|
238
|
|
|
$field = new DriverFieldDrupal8( |
|
239
|
|
|
$values, |
|
240
|
|
|
$fieldName, |
|
241
|
|
|
$this->type, |
|
242
|
|
|
$this->bundle, |
|
243
|
|
|
$this->projectPluginRoot, |
|
244
|
|
|
$this->fieldPluginManager |
|
245
|
|
|
); |
|
246
|
|
|
return $field; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Get the entity type storage. |
|
251
|
|
|
* |
|
252
|
|
|
* @return \Drupal\Core\Entity\EntityStorageInterface |
|
253
|
|
|
*/ |
|
254
|
|
|
protected function getStorage() |
|
255
|
|
|
{ |
|
256
|
|
|
return $this->storage; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Get a new entity object. |
|
261
|
|
|
* |
|
262
|
|
|
* @return \Drupal\Core\Entity\EntityInterface |
|
263
|
|
|
*/ |
|
264
|
|
|
protected function getNewEntity() |
|
265
|
|
|
{ |
|
266
|
|
|
$values = []; |
|
267
|
|
|
// Set the bundle as a field if not simply using the default for |
|
268
|
|
|
// a bundle-less entity type. |
|
269
|
|
|
if ($this->type !== $this->bundle) { |
|
270
|
|
|
$bundleKey = $this->getBundleKey(); |
|
271
|
|
|
$values[$bundleKey] = $this->bundle; |
|
272
|
|
|
} |
|
273
|
|
|
$entity = $this->getStorage()->create($values); |
|
274
|
|
|
return $entity; |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.