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

EntityBrowser::addWidget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

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