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