Completed
Pull Request — 8.x-1.x (#127)
by Janez
07:37
created

EntityBrowser::getWidgetSelector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

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