1 | <?php |
||
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() { |
||
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) { |
||
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) { |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | public function setDisplay($display) { |
||
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | public function setWidgetSelector($widget_selector) { |
||
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() { |
||
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) { |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | public function getFirstWidget() { |
||
305 | $instance_ids = $this->getWidgets()->getInstanceIds(); |
||
306 | return reset($instance_ids); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function addAdditionalWidgetParameters(array $parameters) { |
||
317 | |||
318 | /** |
||
319 | * {@inheritdoc} |
||
320 | */ |
||
321 | public function getAdditionalWidgetParameters() { |
||
325 | |||
326 | /** |
||
327 | * Returns selection display plugin collection. |
||
328 | * |
||
329 | * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
||
330 | * The tag plugin collection. |
||
331 | */ |
||
332 | protected function selectionDisplayPluginCollection() { |
||
339 | |||
340 | /** |
||
341 | * {@inheritdoc} |
||
342 | */ |
||
343 | public function getSelectionDisplay() { |
||
346 | |||
347 | /** |
||
348 | * Returns widget selector plugin collection. |
||
349 | * |
||
350 | * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
||
351 | * The tag plugin collection. |
||
352 | */ |
||
353 | protected function widgetSelectorPluginCollection() { |
||
354 | if (!$this->widgetSelectorCollection) { |
||
355 | $options = array(); |
||
356 | foreach ($this->getWidgets()->getInstanceIds() as $id) { |
||
357 | $options[$id] = $this->getWidgets()->get($id)->label(); |
||
358 | } |
||
359 | $this->widget_selector_configuration['widget_ids'] = $options; |
||
360 | $this->widgetSelectorCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.widget_selector'), $this->widget_selector, $this->widget_selector_configuration); |
||
361 | } |
||
362 | return $this->widgetSelectorCollection; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * {@inheritdoc} |
||
367 | */ |
||
368 | public function getWidgetSelector() { |
||
371 | |||
372 | /** |
||
373 | * {@inheritdoc} |
||
374 | */ |
||
375 | public function route() { |
||
376 | // TODO: Allow displays to define more than just path. |
||
377 | // See: https://www.drupal.org/node/2364193 |
||
378 | $display = $this->getDisplay(); |
||
379 | if ($display instanceof DisplayRouterInterface) { |
||
380 | $path = $display->path(); |
||
381 | return new Route( |
||
382 | $path, |
||
383 | [ |
||
384 | '_controller' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::getContentResult', |
||
385 | '_title_callback' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::title', |
||
386 | 'entity_browser_id' => $this->id(), |
||
387 | ], |
||
388 | ['_permission' => 'access ' . $this->id() . ' entity browser pages'], |
||
389 | ['_admin_route' => \Drupal::config('node.settings')->get('use_admin_theme')] |
||
390 | ); |
||
391 | } |
||
392 | |||
393 | return FALSE; |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * {@inheritdoc} |
||
398 | */ |
||
399 | public function preSave(EntityStorageInterface $storage) { |
||
400 | parent::preSave($storage); |
||
401 | |||
402 | // Entity browser ID was added when creating. No need to save that as it can |
||
403 | // always be calculated. |
||
404 | foreach ($this->widgets as &$widget) { |
||
405 | unset($widget['settings']['entity_browser_id']); |
||
406 | } |
||
407 | unset($this->selection_display_configuration['entity_browser_id']); |
||
408 | unset($this->display_configuration['entity_browser_id']); |
||
409 | unset($this->widget_selector_configuration['widget_ids']); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Prevent plugin collections from being serialized and correctly serialize |
||
414 | * selected entities. |
||
415 | */ |
||
416 | public function __sleep() { |
||
417 | // Save configuration for all plugins. |
||
418 | $this->widgets = $this->getWidgets()->getConfiguration(); |
||
419 | $this->widget_selector_configuration = $this->widgetSelectorPluginCollection()->getConfiguration(); |
||
420 | $this->display_configuration = $this->displayPluginCollection()->getConfiguration(); |
||
421 | $this->selection_display_configuration = $this->selectionDisplayPluginCollection()->getConfiguration(); |
||
422 | |||
423 | return array_diff( |
||
424 | array_keys(get_object_vars($this)), |
||
425 | [ |
||
426 | 'widgetsCollection', |
||
427 | 'widgetSelectorCollection', |
||
428 | 'displayCollection', |
||
429 | 'selectionDisplayCollection', |
||
430 | 'selectedEntities', |
||
431 | ] |
||
432 | ); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * {@inheritdoc} |
||
437 | */ |
||
438 | public function save() { |
||
445 | |||
446 | /** |
||
447 | * {@inheritdoc} |
||
448 | */ |
||
449 | public function getFormObject() { |
||
454 | |||
455 | /** |
||
456 | * {@inheritdoc} |
||
457 | */ |
||
458 | protected function urlRouteParameters($rel) { |
||
467 | |||
468 | } |
||
469 |