Completed
Pull Request — 8.x-1.x (#126)
by Janez
01:59
created

EntityBrowser   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 416
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 12
Bugs 0 Features 1
Metric Value
wmc 36
c 12
b 0
f 1
lcom 3
cbo 2
dl 0
loc 416
rs 8.8

27 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 3 1
A setName() 0 4 1
A getDisplay() 0 3 1
A setLabel() 0 4 1
A getWidget() 0 3 1
A getAdditionalWidgetParameters() 0 4 1
A getSelectionDisplay() 0 3 1
A getWidgetSelector() 0 3 1
A getName() 0 3 1
A setDisplay() 0 6 1
A setWidgetSelector() 0 6 1
A setSelectionDisplay() 0 6 1
A displayPluginCollection() 0 7 2
A getPluginCollections() 0 8 1
A getWidgets() 0 10 3
A addWidget() 0 5 1
A deleteWidget() 0 5 1
A getFirstWidget() 0 4 1
A addAdditionalWidgetParameters() 0 5 1
A selectionDisplayPluginCollection() 0 7 2
A widgetSelectorPluginCollection() 0 11 3
A route() 0 20 2
A preSave() 0 12 2
A __sleep() 0 18 1
A save() 0 7 1
A getFormObject() 0 5 1
A urlRouteParameters() 0 13 2
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
 *       "delete" = "Drupal\entity_browser\Form\EntityBrowserDeleteForm",
29
 *     },
30
 *     "access" = "Drupal\Core\Entity\EntityAccessControlHandler",
31
 *     "list_builder" = "Drupal\entity_browser\Controllers\EntityBrowserListBuilder",
32
 *     "wizard" = {
33
 *       "add" = "Drupal\entity_browser\Wizard\EntityBrowserWizardAdd",
34
 *       "edit" = "Drupal\entity_browser\Wizard\EntityBrowserWizard"
35
 *     }
36
 *   },
37
 *   links = {
38
 *     "canonical" = "/admin/config/content/entity_browser/{machine_name}/{step}",
39
 *     "collection" = "/admin/config/content/entity_browser",
40
 *     "edit-form" = "/admin/config/content/entity_browser/{machine_name}/{step}",
41
 *     "delete-form" = "/admin/config/content/entity_browser/{entity_browser}/delete",
42
 *   },
43
 *   admin_permission = "administer entity browsers",
44
 *   config_prefix = "browser",
45
 *   entity_keys = {
46
 *     "id" = "name",
47
 *     "label" = "label"
48
 *   },
49
 *   config_export = {
50
 *     "name",
51
 *     "label",
52
 *     "display",
53
 *     "display_configuration",
54
 *     "selection_display",
55
 *     "selection_display_configuration",
56
 *     "widget_selector",
57
 *     "widget_selector_configuration",
58
 *     "widgets",
59
 *   },
60
 * )
61
 */
62
class EntityBrowser extends ConfigEntityBase implements EntityBrowserInterface, EntityWithPluginCollectionInterface {
63
64
  /**
65
   * The name of the entity browser.
66
   *
67
   * @var string
68
   */
69
  public $name;
70
71
  /**
72
   * The entity browser label.
73
   *
74
   * @var string
75
   */
76
  public $label;
77
78
  /**
79
   * The display plugin id.
80
   *
81
   * @var string
82
   */
83
  public $display;
84
85
  /**
86
   * The display plugin configuration.
87
   *
88
   * @var array
89
   */
90
  public $display_configuration = [];
91
92
  /**
93
   * Display lazy plugin collection.
94
   *
95
   * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
96
   */
97
  protected $displayCollection;
98
99
  /**
100
   * The array of widgets for this entity browser.
101
   *
102
   * @var array
103
   */
104
  protected $widgets = [];
105
106
  /**
107
   * Holds the collection of widgets that are used by this entity browser.
108
   *
109
   * @var \Drupal\entity_browser\WidgetsCollection
110
   */
111
  protected $widgetsCollection;
112
113
  /**
114
   * The selection display plugin ID.
115
   *
116
   * @var string
117
   */
118
  public $selection_display;
119
120
  /**
121
   * The selection display plugin configuration.
122
   *
123
   * @var array
124
   */
125
  public $selection_display_configuration = [];
126
127
  /**
128
   * Selection display plugin collection.
129
   *
130
   * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
131
   */
132
  protected $selectionDisplayCollection;
133
134
  /**
135
   * The widget selector plugin ID.
136
   *
137
   * @var string
138
   */
139
  public $widget_selector;
140
141
  /**
142
   * The widget selector plugin configuration.
143
   *
144
   * @var array
145
   */
146
  public $widget_selector_configuration = [];
147
148
  /**
149
   * Widget selector plugin collection.
150
   *
151
   * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
152
   */
153
  protected $widgetSelectorCollection;
154
155
  /**
156
   * Additional widget parameters.
157
   *
158
   * @var array
159
   */
160
  protected $additional_widget_parameters = [];
161
162
  /**
163
   * Name of the form class.
164
   *
165
   * @var string
166
   */
167
  protected $form_class = '\Drupal\entity_browser\Form\EntityBrowserForm';
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->uuid());
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\EntityBrowserFormController::getContentResult',
389
          '_title_callback' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::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
  /**
451
   * {@inheritdoc}
452
   */
453
  public function getFormObject() {
454
    $form_class = \Drupal::service('class_resolver')->getInstanceFromDefinition($this->form_class);
455
    $form_class->setEntityBrowser($this);
456
    return $form_class;
457
  }
458
459
460
  /**
461
   * {@inheritdoc}
462
   */
463
  protected function urlRouteParameters($rel) {
464
    $uri_route_parameters = parent::urlRouteParameters($rel);
465
466
    // Form wizard expects step argument and uses machine_name instead of
467
    // entity_browser.
468
    if ($rel == 'edit-form') {
469
      $uri_route_parameters['step'] = 'general';
470
      $uri_route_parameters['machine_name'] = $uri_route_parameters['entity_browser'];
471
      unset($uri_route_parameters['entity_browser']);
472
    }
473
474
    return $uri_route_parameters;
475
  }
476
477
}
478