EntityBrowser::selectionDisplayPluginCollection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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
 *   },
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
   * Name of the form class.
160
   *
161
   * @var string
162
   */
163
  protected $form_class = '\Drupal\entity_browser\Form\EntityBrowserForm';
164
165
  /**
166
   * {@inheritdoc}
167
   */
168
  public function id() {
169
    return $this->name;
170
  }
171
172
  /**
173
   * {@inheritdoc}
174
   */
175
  public function getName() {
176
    return $this->get('name');
177
  }
178
179
  /**
180
   * {@inheritdoc}
181
   */
182
  public function setName($name) {
183
    $this->name = $name;
184
    return $this;
185
  }
186
187
  /**
188
   * {@inheritdoc}
189
   */
190
  public function getDisplay() {
191
    return $this->displayPluginCollection()->get($this->display);
192
  }
193
194
  /**
195
   * {@inheritdoc}
196
   */
197
  public function setLabel($label) {
198
    $this->label = $label;
199
    return $this;
200
  }
201
202
  /**
203
   * {@inheritdoc}
204
   */
205
  public function setDisplay($display) {
206
    $this->display = $display;
207
    $this->displayPluginCollection = NULL;
208
    $this->getDisplay();
209
    return $this;
210
  }
211
212
  /**
213
   * {@inheritdoc}
214
   */
215
  public function setWidgetSelector($widget_selector) {
216
    $this->widget_selector = $widget_selector;
217
    $this->widgetSelectorCollection = NULL;
218
    $this->getWidgetSelector();
219
    return $this;
220
  }
221
222
  /**
223
   * {@inheritdoc}
224
   */
225
  public function setSelectionDisplay($selection_display) {
226
    $this->selection_display = $selection_display;
227
    $this->selectionDisplayCollection = NULL;
228
    $this->getSelectionDisplay();
229
    return $this;
230
  }
231
232
  /**
233
   * Returns display plugin collection.
234
   *
235
   * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
236
   *   The tag plugin collection.
237
   */
238
  protected function displayPluginCollection() {
239
    if (!$this->displayCollection) {
240
      $this->display_configuration['entity_browser_id'] = $this->id();
241
      $this->displayCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.display'), $this->display, $this->display_configuration);
242
    }
243
    return $this->displayCollection;
244
  }
245
246
  /**
247
   * Returns the plugin collections used by this entity.
248
   *
249
   * @return \Drupal\Component\Plugin\LazyPluginCollection[]
250
   *   An array of plugin collections, keyed by the property name they use to
251
   *   store their configuration.
252
   */
253
  public function getPluginCollections() {
254
    return [
255
      'widgets' => $this->getWidgets(),
256
      'widget_selector_configuration' => $this->widgetSelectorPluginCollection(),
257
      'display_configuration' => $this->displayPluginCollection(),
258
      'selection_display_configuration' => $this->selectionDisplayPluginCollection(),
259
    ];
260
  }
261
262
  /**
263
   * {@inheritdoc}
264
   */
265
  public function getWidget($widget) {
266
    return $this->getWidgets()->get($widget);
267
  }
268
269
  /**
270
   * {@inheritdoc}
271
   */
272
  public function getWidgets() {
273
    if (!$this->widgetsCollection) {
274
      foreach ($this->widgets as &$widget) {
275
        $widget['settings']['entity_browser_id'] = $this->id();
276
      }
277
      $this->widgetsCollection = new WidgetsCollection(\Drupal::service('plugin.manager.entity_browser.widget'), $this->widgets);
278
      $this->widgetsCollection->sort();
279
    }
280
    return $this->widgetsCollection;
281
  }
282
283
  /**
284
   * {@inheritdoc}
285
   */
286
  public function addWidget(array $configuration) {
287
    $configuration['uuid'] = $this->uuidGenerator()->generate();
288
    $this->getWidgets()->addInstanceId($configuration['uuid'], $configuration);
289
    return $configuration['uuid'];
290
  }
291
292
  /**
293
   * {@inheritdoc}
294
   */
295
  public function deleteWidget(WidgetInterface $widget) {
296
    $this->getWidgets()->removeInstanceId($widget->uuid());
297
    $this->save();
298
    return $this;
299
  }
300
301
  /**
302
   * {@inheritdoc}
303
   */
304
  public function getFirstWidget() {
305
    $instance_ids = $this->getWidgets()->getInstanceIds();
306
    $instance_ids = array_filter($instance_ids, function ($id) {
307
      return $this->getWidget($id)->access()->isAllowed();
308
    });
309
310
    if (empty($instance_ids)) {
311
      return NULL;
312
    }
313
314
    return reset($instance_ids);
315
  }
316
317
  /**
318
   * {@inheritdoc}
319
   */
320
  public function addAdditionalWidgetParameters(array $parameters) {
321
    // TODO - this doesn't make much sense. Refactor.
322
    $this->additional_widget_parameters += $parameters;
323
    return $this;
324
  }
325
326
  /**
327
   * {@inheritdoc}
328
   */
329
  public function getAdditionalWidgetParameters() {
330
    // TODO - this doesn't make much sense. Refactor.
331
    return $this->get('additional_widget_parameters');
332
  }
333
334
  /**
335
   * Returns selection display plugin collection.
336
   *
337
   * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
338
   *   The tag plugin collection.
339
   */
340
  protected function selectionDisplayPluginCollection() {
341
    if (!$this->selectionDisplayCollection) {
342
      $this->selection_display_configuration['entity_browser_id'] = $this->id();
343
      $this->selectionDisplayCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.selection_display'), $this->selection_display, $this->selection_display_configuration);
344
    }
345
    return $this->selectionDisplayCollection;
346
  }
347
348
  /**
349
   * {@inheritdoc}
350
   */
351
  public function getSelectionDisplay() {
352
    return $this->selectionDisplayPluginCollection()->get($this->selection_display);
353
  }
354
355
  /**
356
   * Returns widget selector plugin collection.
357
   *
358
   * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
359
   *   The tag plugin collection.
360
   */
361
  protected function widgetSelectorPluginCollection() {
362
    if (!$this->widgetSelectorCollection) {
363
      $options = [];
364
      foreach ($this->getWidgets()->getInstanceIds() as $id) {
365
        $options[$id] = $this->getWidgets()->get($id)->label();
366
      }
367
      $this->widget_selector_configuration['widget_ids'] = $options;
368
      $this->widgetSelectorCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.widget_selector'), $this->widget_selector, $this->widget_selector_configuration);
369
    }
370
    return $this->widgetSelectorCollection;
371
  }
372
373
  /**
374
   * {@inheritdoc}
375
   */
376
  public function getWidgetSelector() {
377
    return $this->widgetSelectorPluginCollection()->get($this->widget_selector);
378
  }
379
380
  /**
381
   * {@inheritdoc}
382
   */
383
  public function route() {
384
    // TODO: Allow displays to define more than just path.
385
    // See: https://www.drupal.org/node/2364193
386
    $display = $this->getDisplay();
387
    if ($display instanceof DisplayRouterInterface) {
388
      $path = $display->path();
389
      return new Route(
390
        $path,
391
        [
392
          '_controller' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::getContentResult',
393
          '_title_callback' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::title',
394
          'entity_browser_id' => $this->id(),
395
        ],
396
        ['_permission' => 'access ' . $this->id() . ' entity browser pages'],
397
        ['_admin_route' => \Drupal::config('node.settings')->get('use_admin_theme')]
398
      );
399
    }
400
401
    return FALSE;
402
  }
403
404
  /**
405
   * {@inheritdoc}
406
   */
407
  public function preSave(EntityStorageInterface $storage) {
408
    parent::preSave($storage);
409
410
    // Entity browser ID was added when creating. No need to save that as it can
411
    // always be calculated.
412
    foreach ($this->widgets as &$widget) {
413
      unset($widget['settings']['entity_browser_id']);
414
    }
415
    unset($this->selection_display_configuration['entity_browser_id']);
416
    unset($this->display_configuration['entity_browser_id']);
417
    unset($this->widget_selector_configuration['widget_ids']);
418
  }
419
420
  /**
421
   * Prevent plugin collections from being serialized and correctly serialize
422
   * selected entities.
423
   */
424
  public function __sleep() {
425
    // Save configuration for all plugins.
426
    $this->widgets = $this->getWidgets()->getConfiguration();
427
    $this->widget_selector_configuration = $this->widgetSelectorPluginCollection()->getConfiguration();
428
    $this->display_configuration = $this->displayPluginCollection()->getConfiguration();
429
    $this->selection_display_configuration = $this->selectionDisplayPluginCollection()->getConfiguration();
430
431
    return array_diff(
432
      array_keys(get_object_vars($this)),
433
      [
434
        'widgetsCollection',
435
        'widgetSelectorCollection',
436
        'displayCollection',
437
        'selectionDisplayCollection',
438
        'selectedEntities',
439
      ]
440
    );
441
  }
442
443
  /**
444
   * {@inheritdoc}
445
   */
446
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
447
    parent::postSave($storage, $update);
448
    // Rebuild route information when browsers that register routes
449
    // are created/updated.
450
    \Drupal::service('router.builder')->setRebuildNeeded();
451
  }
452
453
  /**
454
   * {@inheritdoc}
455
   */
456
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
457
    parent::postDelete($storage, $entities);
458
    // Rebuild route information when browsers that register routes
459
    // are deleted.
460
    \Drupal::service('router.builder')->setRebuildNeeded();
461
  }
462
463
  /**
464
   * {@inheritdoc}
465
   */
466
  public function getFormObject() {
467
    $form_class = \Drupal::service('class_resolver')->getInstanceFromDefinition($this->form_class);
468
    $form_class->setEntityBrowser($this);
469
    return $form_class;
470
  }
471
472
  /**
473
   * {@inheritdoc}
474
   */
475
  protected function urlRouteParameters($rel) {
476
    $uri_route_parameters = parent::urlRouteParameters($rel);
477
478
    if ($rel == 'config-translation-overview') {
479
      $uri_route_parameters['step'] = 'general';
480
    }
481
482
    return $uri_route_parameters;
483
  }
484
485
}
486