1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Contains \Drupal\entity_browser\WidgetBase. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Drupal\entity_browser; |
8
|
|
|
|
9
|
|
|
use Drupal\Core\Plugin\PluginBase; |
10
|
|
|
use Drupal\Core\Entity\EntityManagerInterface; |
11
|
|
|
use Drupal\Core\Form\FormStateInterface; |
12
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
13
|
|
|
use Drupal\entity_browser\Events\EntitySelectionEvent; |
14
|
|
|
use Drupal\entity_browser\Events\Events; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Base class for widget plugins. |
20
|
|
|
*/ |
21
|
|
|
abstract class WidgetBase extends PluginBase implements WidgetInterface, ContainerFactoryPluginInterface { |
22
|
|
|
|
23
|
|
|
use PluginConfigurationFormTrait; |
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
|
|
|
* Constructs widget plugin. |
68
|
|
|
* |
69
|
|
|
* @param array $configuration |
70
|
|
|
* A configuration array containing information about the plugin instance. |
71
|
|
|
* @param string $plugin_id |
72
|
|
|
* The plugin_id for the plugin instance. |
73
|
|
|
* @param mixed $plugin_definition |
74
|
|
|
* The plugin implementation definition. |
75
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
76
|
|
|
* Event dispatcher service. |
77
|
|
|
*/ |
78
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityManagerInterface $entity_manager) { |
79
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
80
|
|
|
$this->eventDispatcher = $event_dispatcher; |
81
|
|
|
$this->entityManager = $entity_manager; |
82
|
|
|
$this->setConfiguration($configuration); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
89
|
|
|
return new static( |
90
|
|
|
$configuration, |
91
|
|
|
$plugin_id, |
92
|
|
|
$plugin_definition, |
93
|
|
|
$container->get('event_dispatcher'), |
94
|
|
|
$container->get('entity.manager') |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function defaultConfiguration() { |
102
|
|
|
return []; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function getConfiguration() { |
109
|
|
|
return [ |
110
|
|
|
'settings' => array_diff_key( |
111
|
|
|
$this->configuration, |
112
|
|
|
['entity_browser_id' => 0] |
113
|
|
|
), |
114
|
|
|
'uuid' => $this->uuid(), |
115
|
|
|
'weight' => $this->getWeight(), |
116
|
|
|
'label' => $this->label(), |
117
|
|
|
'id' => $this->id(), |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function setConfiguration(array $configuration) { |
125
|
|
|
$configuration += [ |
126
|
|
|
'settings' => [], |
127
|
|
|
'uuid' => '', |
128
|
|
|
'weight' => '', |
129
|
|
|
'label' => '', |
130
|
|
|
'id' => '', |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$this->configuration = $configuration['settings'] + $this->defaultConfiguration(); |
134
|
|
|
$this->label = $configuration['label']; |
135
|
|
|
$this->weight = $configuration['weight']; |
136
|
|
|
$this->uuid = $configuration['uuid']; |
137
|
|
|
$this->id = $configuration['id']; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function calculateDependencies() { |
144
|
|
|
return []; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function id() { |
151
|
|
|
return $this->id; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function uuid() { |
158
|
|
|
return $this->uuid; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function label() { |
165
|
|
|
return $this->label; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function setLabel($label) { |
172
|
|
|
$this->label = $label; |
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
|
|
public function getWeight() { |
180
|
|
|
return $this->weight; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function setWeight($weight) { |
187
|
|
|
$this->weight = $weight; |
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function validate(array &$form, FormStateInterface $form_state) {} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function submit(array &$element, array &$form, FormStateInterface $form_state) {} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Dispatches event that informs all subscribers about new selected entities. |
203
|
|
|
* |
204
|
|
|
* @param array $entities |
205
|
|
|
* Array of entities. |
206
|
|
|
*/ |
207
|
|
|
protected function selectEntities(array $entities, FormStateInterface $form_state) { |
208
|
|
|
$selected_entities = &$form_state->get(['entity_browser', 'selected_entities']); |
209
|
|
|
$selected_entities = array_merge($selected_entities, $entities); |
210
|
|
|
|
211
|
|
|
$this->eventDispatcher->dispatch( |
212
|
|
|
Events::SELECTED, |
213
|
|
|
new EntitySelectionEvent( |
214
|
|
|
$this->configuration['entity_browser_id'], |
215
|
|
|
$form_state->get(['entity_browser', 'instance_uuid']), |
216
|
|
|
$entities |
217
|
|
|
)); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|