Completed
Pull Request — 8.x-1.x (#137)
by
unknown
03:23
created

EntityBrowser::widgetSelectorPluginCollection()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

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