|
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
|
|
|
abstract class DriverEntityPluginBase extends PluginBase implements DriverEntityPluginInterface, DriverEntityInterface |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Entity type's machine name. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $type; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Entity bundle's machine name. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $bundle; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Entity type definition. |
|
36
|
|
|
* |
|
37
|
|
|
* @var \Drupal\Core\Entity\EntityTypeInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $typeDefinition; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Entity type definition. |
|
43
|
|
|
* |
|
44
|
|
|
* @var \Drupal\Core\Entity\EntityStorageInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $storage; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The saved Drupal entity this object is wrapping for the Driver. |
|
50
|
|
|
* |
|
51
|
|
|
* @var \Drupal\Core\Entity\EntityInterface; |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $entity; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The driver field plugin manager. |
|
57
|
|
|
* |
|
58
|
|
|
* @var \Drupal\Driver\Plugin\DriverPluginManagerInterface; |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $fieldPluginManager; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* The directory to search for additional project-specific driver plugins. |
|
64
|
|
|
* |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $projectPluginRoot; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct( |
|
73
|
|
|
array $configuration, |
|
74
|
|
|
$plugin_id, |
|
75
|
|
|
$plugin_definition |
|
76
|
|
|
) { |
|
77
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
|
78
|
|
|
|
|
79
|
|
|
if (!is_string($configuration['type'])) { |
|
80
|
|
|
throw new \Exception("Entity type is required to initiate entity plugin."); |
|
81
|
|
|
} |
|
82
|
|
|
$this->type = $configuration['type']; |
|
83
|
|
|
$this->bundle = $configuration['bundle']; |
|
84
|
|
|
if (isset($configuration['fieldPluginManager'])) { |
|
85
|
|
|
$this->fieldPluginManager = $configuration['fieldPluginManager']; |
|
86
|
|
|
} |
|
87
|
|
|
if (isset($configuration['projectPluginRoot'])) { |
|
88
|
|
|
$this->projectPluginRoot = $configuration['projectPluginRoot']; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function create( |
|
96
|
|
|
ContainerInterface $container, |
|
97
|
|
|
array $configuration, |
|
98
|
|
|
$plugin_id, |
|
99
|
|
|
$plugin_definition |
|
100
|
|
|
) { |
|
101
|
|
|
return new static( |
|
102
|
|
|
$configuration, |
|
103
|
|
|
$plugin_id, |
|
104
|
|
|
$plugin_definition |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function __call($name, $arguments) |
|
112
|
|
|
{ |
|
113
|
|
|
// Forward unknown calls to the entity. |
|
114
|
|
|
if (!$this->hasEntity()) { |
|
115
|
|
|
throw new \Exception("Method '$name' unknown on Driver entity plugin and entity not yet available."); |
|
116
|
|
|
} else { |
|
117
|
|
|
if (method_exists($this->getEntity(), $name)) { |
|
118
|
|
|
return call_user_func_array(array($this->getEntity(), $name), $arguments); |
|
119
|
|
|
} |
|
120
|
|
|
throw new \Exception("Method '$name' unknown on both Driver entity plugin and attached Drupal entity."); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
|
|
public function __get($name) |
|
128
|
|
|
{ |
|
129
|
|
|
// Forward unknown gets to the entity. |
|
130
|
|
|
if (!$this->hasEntity()) { |
|
131
|
|
|
throw new \Exception("Property '$name' unknown on Driver entity plugin and entity not yet available."); |
|
132
|
|
|
} else { |
|
133
|
|
|
if (property_exists($this->getEntity(), $name)) { |
|
134
|
|
|
return $this->getEntity()->$name; |
|
135
|
|
|
} |
|
136
|
|
|
throw new \Exception("Property '$name' unknown on both Driver entity plugin and attached Drupal entity."); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getEntity() |
|
144
|
|
|
{ |
|
145
|
|
|
if (!$this->hasEntity()) { |
|
146
|
|
|
$this->entity = $this->getNewEntity(); |
|
147
|
|
|
} |
|
148
|
|
|
return $this->entity; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritdoc} |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getLabelKeys() |
|
155
|
|
|
{ |
|
156
|
|
|
if (isset($this->pluginDefinition['labelKeys'])) { |
|
157
|
|
|
return $this->pluginDefinition['labelKeys']; |
|
158
|
|
|
} |
|
159
|
|
|
return []; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
|
|
public function isNew() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->hasEntity(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritdoc} |
|
172
|
|
|
*/ |
|
173
|
|
|
public function tearDown() |
|
174
|
|
|
{ |
|
175
|
|
|
$this->delete(); |
|
176
|
|
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* {@inheritdoc} |
|
181
|
|
|
*/ |
|
182
|
|
|
public function setFields($fields) |
|
183
|
|
|
{ |
|
184
|
|
|
foreach ($fields as $identifier => $field) { |
|
185
|
|
|
$this->set($identifier, $field); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* {@inheritdoc} |
|
191
|
|
|
*/ |
|
192
|
|
|
public function supportsBundles() |
|
193
|
|
|
{ |
|
194
|
|
|
// In D8 bundle key is returned as empty but not null if entity type has |
|
195
|
|
|
// no bundle key. |
|
196
|
|
|
return !(empty($this->getBundleKey())); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Get the driver field plugin manager. |
|
201
|
|
|
* |
|
202
|
|
|
* @return \Drupal\Driver\Plugin\DriverPluginManagerInterface |
|
203
|
|
|
* The driver field plugin manager |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function getFieldPluginManager() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->fieldPluginManager; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Whether a Drupal entity has already been instantiated and attached. |
|
212
|
|
|
* |
|
213
|
|
|
* @return boolean |
|
214
|
|
|
* Whether a Drupal entity is already attached to this plugin. |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function hasEntity() |
|
217
|
|
|
{ |
|
218
|
|
|
return !is_null($this->entity); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|