1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Contains \Drupal\entity_browser\WidgetBase. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Drupal\entity_browser; |
8
|
|
|
|
9
|
|
|
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface; |
10
|
|
|
use Drupal\Core\Plugin\PluginBase; |
11
|
|
|
use Drupal\Core\Entity\EntityManagerInterface; |
12
|
|
|
use Drupal\Core\Form\FormStateInterface; |
13
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
14
|
|
|
use Drupal\entity_browser\Events\EntitySelectionEvent; |
15
|
|
|
use Drupal\entity_browser\Events\Events; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Base class for widget plugins. |
22
|
|
|
*/ |
23
|
|
|
abstract class WidgetBase extends PluginBase implements WidgetInterface, ContainerFactoryPluginInterface { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Plugin id. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $id; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Plugin uuid. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $uuid; |
38
|
|
|
/** |
39
|
|
|
* Plugin label. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $label; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Plugin weight. |
47
|
|
|
* |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $weight; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Event dispatcher service. |
54
|
|
|
* |
55
|
|
|
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
56
|
|
|
*/ |
57
|
|
|
protected $eventDispatcher; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Entity manager service. |
61
|
|
|
* |
62
|
|
|
* @var \Drupal\Core\Entity\EntityManagerInterface |
63
|
|
|
*/ |
64
|
|
|
protected $entityManager; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The Expirable key value store. |
68
|
|
|
* |
69
|
|
|
* @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface |
70
|
|
|
*/ |
71
|
|
|
private $keyValue; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The Request object. |
75
|
|
|
* |
76
|
|
|
* @var \Symfony\Component\HttpFoundation\Request |
77
|
|
|
*/ |
78
|
|
|
private $request; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Constructs widget plugin. |
82
|
|
|
* |
83
|
|
|
* @param array $configuration |
84
|
|
|
* A configuration array containing information about the plugin instance. |
85
|
|
|
* @param string $plugin_id |
86
|
|
|
* The plugin_id for the plugin instance. |
87
|
|
|
* @param mixed $plugin_definition |
88
|
|
|
* The plugin implementation definition. |
89
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
90
|
|
|
* Event dispatcher service. |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityManagerInterface $entity_manager, KeyValueStoreExpirableInterface $key_value, Request $request) { |
|
|
|
|
93
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
94
|
|
|
$this->eventDispatcher = $event_dispatcher; |
95
|
|
|
$this->entityManager = $entity_manager; |
96
|
|
|
$this->setConfiguration($configuration); |
97
|
|
|
$this->keyValue = $key_value; |
98
|
|
|
$this->request = $request; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
|
|
|
105
|
|
|
return new static( |
106
|
|
|
$configuration, |
107
|
|
|
$plugin_id, |
108
|
|
|
$plugin_definition, |
109
|
|
|
$container->get('event_dispatcher'), |
110
|
|
|
$container->get('entity.manager'), |
111
|
|
|
$container->get('keyvalue.expirable')->get('entity_browser'), |
112
|
|
|
$container->get('request_stack')->getCurrentRequest() |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function defaultConfiguration() { |
120
|
|
|
return []; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function getConfiguration() { |
127
|
|
|
return [ |
128
|
|
|
'settings' => $this->configuration, |
129
|
|
|
'uuid' => $this->uuid(), |
130
|
|
|
'weight' => $this->getWeight(), |
131
|
|
|
'label' => $this->label(), |
132
|
|
|
'id' => $this->id(), |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function setConfiguration(array $configuration) { |
140
|
|
|
$configuration += [ |
141
|
|
|
'settings' => [], |
142
|
|
|
'uuid' => '', |
143
|
|
|
'weight' => '', |
144
|
|
|
'label' => '', |
145
|
|
|
'id' => '', |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
$this->configuration = $configuration['settings'] + $this->defaultConfiguration(); |
149
|
|
|
$this->label = $configuration['label']; |
150
|
|
|
$this->weight = $configuration['weight']; |
151
|
|
|
$this->uuid = $configuration['uuid']; |
152
|
|
|
$this->id = $configuration['id']; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function calculateDependencies() { |
159
|
|
|
return []; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function id() { |
166
|
|
|
return $this->id; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function uuid() { |
173
|
|
|
return $this->uuid; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
|
|
public function label() { |
180
|
|
|
return $this->label; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function getWeight() { |
187
|
|
|
return $this->weight; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
|
|
public function setWeight($weight) { |
194
|
|
|
$this->weight = $weight; |
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
|
|
public function validate(array &$form, FormStateInterface $form_state) {} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
|
|
public function runWidgetValidators(array $entities, $validators = []) { |
208
|
|
|
// @todo Implement a centralized way to get arguments from path for all widgets? |
209
|
|
|
if ($validators_hash = $this->request->get('validators')) { |
210
|
|
|
$passed_validators = $this->keyValue->get($validators_hash); |
211
|
|
|
|
212
|
|
|
if (!empty($passed_validators)) { |
213
|
|
|
$validators += $passed_validators; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
foreach ($validators as $validator_id => $options) { |
218
|
|
|
/** @var \Drupal\entity_browser\WidgetValidationInterface $widget_validator */ |
219
|
|
|
$widget_validator = \Drupal::service('plugin.manager.entity_browser.widget_validation')->createInstance($validator_id, []); |
220
|
|
|
return $widget_validator->validate($entities, $options); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
|
|
public function submit(array &$element, array &$form, FormStateInterface $form_state) {} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Dispatches event that informs all subscribers about new selected entities. |
231
|
|
|
* |
232
|
|
|
* @param array $entities |
233
|
|
|
* Array of entities. |
234
|
|
|
*/ |
235
|
|
|
protected function selectEntities(array $entities, FormStateInterface $form_state) { |
236
|
|
|
$selected_entities = &$form_state->get(['entity_browser', 'selected_entities']); |
237
|
|
|
$selected_entities = array_merge($selected_entities, $entities); |
238
|
|
|
|
239
|
|
|
$this->eventDispatcher->dispatch( |
240
|
|
|
Events::SELECTED, |
241
|
|
|
new EntitySelectionEvent( |
242
|
|
|
$this->configuration['entity_browser_id'], |
243
|
|
|
$form_state->get(['entity_browser', 'instance_uuid']), |
244
|
|
|
$entities |
245
|
|
|
)); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.