1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Contains \Drupal\entity_browser\Entity\EntityBrowser. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Drupal\entity_browser\Entity; |
9
|
|
|
|
10
|
|
|
use Drupal\Core\Config\Entity\ConfigEntityBase; |
11
|
|
|
use Drupal\Core\Entity\EntityStorageInterface; |
12
|
|
|
use Drupal\Core\Entity\EntityWithPluginCollectionInterface; |
13
|
|
|
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection; |
14
|
|
|
use Drupal\entity_browser\EntityBrowserInterface; |
15
|
|
|
use Drupal\entity_browser\Events\EntitySelectionEvent; |
16
|
|
|
use Drupal\entity_browser\Events\Events; |
17
|
|
|
use Drupal\entity_browser\Events\SelectionDoneEvent; |
18
|
|
|
use Drupal\entity_browser\WidgetInterface; |
19
|
|
|
use Drupal\entity_browser\DisplayRouterInterface; |
20
|
|
|
use Drupal\entity_browser\WidgetsCollection; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
use Symfony\Component\Routing\Route; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Defines an entity browser configuration entity. |
26
|
|
|
* |
27
|
|
|
* @ConfigEntityType( |
28
|
|
|
* id = "entity_browser", |
29
|
|
|
* label = @Translation("Entity browser"), |
30
|
|
|
* handlers = { |
31
|
|
|
* "form" = { |
32
|
|
|
* "entity_browser" = "Drupal\entity_browser\Form\EntityBrowserForm" |
33
|
|
|
* }, |
34
|
|
|
* "wizard" = { |
35
|
|
|
* "add" = "Drupal\entity_browser\Wizard\EntityBrowserWizardAdd", |
36
|
|
|
* "edit" = "Drupal\entity_browser\Wizard\EntityBrowserWizard" |
37
|
|
|
* } |
38
|
|
|
* }, |
39
|
|
|
* admin_permission = "administer entity browsers", |
40
|
|
|
* config_prefix = "browser", |
41
|
|
|
* entity_keys = { |
42
|
|
|
* "id" = "name", |
43
|
|
|
* "label" = "label" |
44
|
|
|
* }, |
45
|
|
|
* config_export = { |
46
|
|
|
* "name", |
47
|
|
|
* "label", |
48
|
|
|
* "display", |
49
|
|
|
* "display_configuration", |
50
|
|
|
* "selection_display", |
51
|
|
|
* "selection_display_configuration", |
52
|
|
|
* "widget_selector", |
53
|
|
|
* "widget_selector_configuration", |
54
|
|
|
* "widgets", |
55
|
|
|
* }, |
56
|
|
|
* ) |
57
|
|
|
*/ |
58
|
|
|
class EntityBrowser extends ConfigEntityBase implements EntityBrowserInterface, EntityWithPluginCollectionInterface { |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The name of the entity browser. |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public $name; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The entity browser label. |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
public $label; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The display plugin id. |
76
|
|
|
* |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
public $display; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The display plugin configuration. |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
public $display_configuration = []; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Display lazy plugin collection. |
90
|
|
|
* |
91
|
|
|
* @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
92
|
|
|
*/ |
93
|
|
|
protected $displayCollection; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* The array of widgets for this entity browser. |
97
|
|
|
* |
98
|
|
|
* @var array |
99
|
|
|
*/ |
100
|
|
|
protected $widgets = []; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Holds the collection of widgets that are used by this entity browser. |
104
|
|
|
* |
105
|
|
|
* @var \Drupal\entity_browser\WidgetsCollection |
106
|
|
|
*/ |
107
|
|
|
protected $widgetsCollection; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* The selection display plugin ID. |
111
|
|
|
* |
112
|
|
|
* @var string |
113
|
|
|
*/ |
114
|
|
|
public $selection_display; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* The selection display plugin configuration. |
118
|
|
|
* |
119
|
|
|
* @var array |
120
|
|
|
*/ |
121
|
|
|
public $selection_display_configuration = []; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Selection display plugin collection. |
125
|
|
|
* |
126
|
|
|
* @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
127
|
|
|
*/ |
128
|
|
|
protected $selectionDisplayCollection; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* The widget selector plugin ID. |
132
|
|
|
* |
133
|
|
|
* @var string |
134
|
|
|
*/ |
135
|
|
|
public $widget_selector; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* The widget selector plugin configuration. |
139
|
|
|
* |
140
|
|
|
* @var array |
141
|
|
|
*/ |
142
|
|
|
public $widget_selector_configuration = []; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Widget selector plugin collection. |
146
|
|
|
* |
147
|
|
|
* @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
148
|
|
|
*/ |
149
|
|
|
protected $widgetSelectorCollection; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Additional widget parameters. |
153
|
|
|
* |
154
|
|
|
* @var array |
155
|
|
|
*/ |
156
|
|
|
protected $additional_widget_parameters = []; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @var |
160
|
|
|
*/ |
161
|
|
|
protected $displayPluginCollection; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function id() { |
167
|
|
|
return $this->name; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function getName() { |
174
|
|
|
return $this->get('name'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function setName($name) { |
181
|
|
|
$this->name = $name; |
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function getDisplay() { |
189
|
|
|
return $this->displayPluginCollection()->get($this->display); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
|
|
public function setLabel($label) { |
196
|
|
|
$this->label = $label; |
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
|
|
public function setDisplay($display) { |
204
|
|
|
$this->display = $display; |
205
|
|
|
$this->displayPluginCollection = NULL; |
206
|
|
|
$this->getDisplay(); |
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
|
|
public function setWidgetSelector($widget_selector) { |
214
|
|
|
$this->widget_selector = $widget_selector; |
215
|
|
|
$this->widgetSelectorCollection = NULL; |
216
|
|
|
$this->getWidgetSelector(); |
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function setSelectionDisplay($selection_display) { |
224
|
|
|
$this->selection_display = $selection_display; |
225
|
|
|
$this->selectionDisplayCollection = NULL; |
226
|
|
|
$this->getSelectionDisplay(); |
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Returns display plugin collection. |
232
|
|
|
* |
233
|
|
|
* @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
234
|
|
|
* The tag plugin collection. |
235
|
|
|
*/ |
236
|
|
|
protected function displayPluginCollection() { |
237
|
|
|
if (!$this->displayCollection) { |
238
|
|
|
$this->display_configuration['entity_browser_id'] = $this->id(); |
239
|
|
|
$this->displayCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.display'), $this->display, $this->display_configuration); |
240
|
|
|
} |
241
|
|
|
return $this->displayCollection; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Returns the plugin collections used by this entity. |
246
|
|
|
* |
247
|
|
|
* @return \Drupal\Component\Plugin\LazyPluginCollection[] |
248
|
|
|
* An array of plugin collections, keyed by the property name they use to |
249
|
|
|
* store their configuration. |
250
|
|
|
*/ |
251
|
|
|
public function getPluginCollections() { |
252
|
|
|
return [ |
253
|
|
|
'widgets' => $this->getWidgets(), |
254
|
|
|
'widget_selector_configuration' => $this->widgetSelectorPluginCollection(), |
255
|
|
|
'display_configuration' => $this->displayPluginCollection(), |
256
|
|
|
'selection_display_configuration' => $this->selectionDisplayPluginCollection(), |
257
|
|
|
]; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* {@inheritdoc} |
262
|
|
|
*/ |
263
|
|
|
public function getWidget($widget) { |
264
|
|
|
return $this->getWidgets()->get($widget); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
|
|
public function getWidgets() { |
271
|
|
|
if (!$this->widgetsCollection) { |
272
|
|
|
foreach ($this->widgets as &$widget) { |
273
|
|
|
$widget['settings']['entity_browser_id'] = $this->id(); |
274
|
|
|
} |
275
|
|
|
$this->widgetsCollection = new WidgetsCollection(\Drupal::service('plugin.manager.entity_browser.widget'), $this->widgets); |
276
|
|
|
$this->widgetsCollection->sort(); |
277
|
|
|
} |
278
|
|
|
return $this->widgetsCollection; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* {@inheritdoc} |
283
|
|
|
*/ |
284
|
|
|
public function addWidget(array $configuration) { |
285
|
|
|
$configuration['uuid'] = $this->uuidGenerator()->generate(); |
286
|
|
|
$this->getWidgets()->addInstanceId($configuration['uuid'], $configuration); |
287
|
|
|
return $configuration['uuid']; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* {@inheritdoc} |
292
|
|
|
*/ |
293
|
|
|
public function deleteWidget(WidgetInterface $widget) { |
294
|
|
|
$this->getWidgets()->removeInstanceId($widget->getUuid()); |
295
|
|
|
$this->save(); |
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* {@inheritdoc} |
301
|
|
|
*/ |
302
|
|
|
public function getFirstWidget() { |
303
|
|
|
$instance_ids = $this->getWidgets()->getInstanceIds(); |
304
|
|
|
return reset($instance_ids); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* {@inheritdoc} |
309
|
|
|
*/ |
310
|
|
|
public function addAdditionalWidgetParameters(array $parameters) { |
311
|
|
|
// TODO - this doesn't make much sense. Refactor. |
312
|
|
|
$this->additional_widget_parameters += $parameters; |
313
|
|
|
return $this; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* {@inheritdoc} |
318
|
|
|
*/ |
319
|
|
|
public function getAdditionalWidgetParameters() { |
320
|
|
|
// TODO - this doesn't make much sense. Refactor. |
321
|
|
|
return $this->get('additional_widget_parameters'); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Returns selection display plugin collection. |
326
|
|
|
* |
327
|
|
|
* @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
328
|
|
|
* The tag plugin collection. |
329
|
|
|
*/ |
330
|
|
|
protected function selectionDisplayPluginCollection() { |
331
|
|
|
if (!$this->selectionDisplayCollection) { |
332
|
|
|
$this->selection_display_configuration['entity_browser_id'] = $this->id(); |
333
|
|
|
$this->selectionDisplayCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.selection_display'), $this->selection_display, $this->selection_display_configuration); |
334
|
|
|
} |
335
|
|
|
return $this->selectionDisplayCollection; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* {@inheritdoc} |
340
|
|
|
*/ |
341
|
|
|
public function getSelectionDisplay() { |
342
|
|
|
return $this->selectionDisplayPluginCollection()->get($this->selection_display); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Returns widget selector plugin collection. |
347
|
|
|
* |
348
|
|
|
* @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
349
|
|
|
* The tag plugin collection. |
350
|
|
|
*/ |
351
|
|
|
protected function widgetSelectorPluginCollection() { |
352
|
|
|
if (!$this->widgetSelectorCollection) { |
353
|
|
|
$options = array(); |
354
|
|
|
foreach ($this->getWidgets()->getInstanceIds() as $id) { |
355
|
|
|
$options[$id] = $this->getWidgets()->get($id)->label(); |
356
|
|
|
} |
357
|
|
|
$this->widget_selector_configuration['widget_ids'] = $options; |
358
|
|
|
$this->widgetSelectorCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.widget_selector'), $this->widget_selector, $this->widget_selector_configuration); |
359
|
|
|
} |
360
|
|
|
return $this->widgetSelectorCollection; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* {@inheritdoc} |
365
|
|
|
*/ |
366
|
|
|
public function getWidgetSelector() { |
367
|
|
|
return $this->widgetSelectorPluginCollection()->get($this->widget_selector); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* {@inheritdoc} |
372
|
|
|
*/ |
373
|
|
|
public function route() { |
374
|
|
|
// TODO: Allow displays to define more than just path. |
375
|
|
|
// See: https://www.drupal.org/node/2364193 |
376
|
|
|
$display = $this->getDisplay(); |
377
|
|
|
if ($display instanceof DisplayRouterInterface) { |
378
|
|
|
$path = $display->path(); |
379
|
|
|
return new Route( |
380
|
|
|
$path, |
381
|
|
|
[ |
382
|
|
|
'_controller' => 'Drupal\entity_browser\Controllers\StandalonePage::page', |
383
|
|
|
'_title_callback' => 'Drupal\entity_browser\Controllers\StandalonePage::title', |
384
|
|
|
'entity_browser_id' => $this->id(), |
385
|
|
|
], |
386
|
|
|
['_permission' => 'access ' . $this->id() . ' entity browser pages'], |
387
|
|
|
['_admin_route' => \Drupal::config('node.settings')->get('use_admin_theme')] |
388
|
|
|
); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return FALSE; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* {@inheritdoc} |
396
|
|
|
*/ |
397
|
|
|
public function preSave(EntityStorageInterface $storage) { |
398
|
|
|
parent::preSave($storage); |
399
|
|
|
|
400
|
|
|
// Entity browser ID was added when creating. No need to save that as it can |
401
|
|
|
// always be calculated. |
402
|
|
|
foreach ($this->widgets as &$widget) { |
403
|
|
|
unset($widget['settings']['entity_browser_id']); |
404
|
|
|
} |
405
|
|
|
unset($this->selection_display_configuration['entity_browser_id']); |
406
|
|
|
unset($this->display_configuration['entity_browser_id']); |
407
|
|
|
unset($this->widget_selector_configuration['widget_ids']); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Prevent plugin collections from being serialized and correctly serialize |
412
|
|
|
* selected entities. |
413
|
|
|
*/ |
414
|
|
|
public function __sleep() { |
415
|
|
|
// Save configuration for all plugins. |
416
|
|
|
$this->widgets = $this->getWidgets()->getConfiguration(); |
417
|
|
|
$this->widget_selector_configuration = $this->widgetSelectorPluginCollection()->getConfiguration(); |
418
|
|
|
$this->display_configuration = $this->displayPluginCollection()->getConfiguration(); |
419
|
|
|
$this->selection_display_configuration = $this->selectionDisplayPluginCollection()->getConfiguration(); |
420
|
|
|
|
421
|
|
|
return array_diff( |
422
|
|
|
array_keys(get_object_vars($this)), |
423
|
|
|
[ |
424
|
|
|
'widgetsCollection', |
425
|
|
|
'widgetSelectorCollection', |
426
|
|
|
'displayCollection', |
427
|
|
|
'selectionDisplayCollection', |
428
|
|
|
'selectedEntities' |
429
|
|
|
] |
430
|
|
|
); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* {@inheritdoc} |
435
|
|
|
*/ |
436
|
|
|
public function save() { |
437
|
|
|
$return = parent::save(); |
438
|
|
|
// Rebuild route information when browsers that register routes |
439
|
|
|
// are created/updated. |
440
|
|
|
\Drupal::service('router.builder')->rebuild(); |
441
|
|
|
return $return; |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
|