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