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