1 | <?php |
||
63 | class EntityBrowser extends ConfigEntityBase implements EntityBrowserInterface, EntityWithPluginCollectionInterface { |
||
64 | |||
65 | /** |
||
66 | * The name of the entity browser. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | public $name; |
||
71 | |||
72 | /** |
||
73 | * The entity browser label. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | public $label; |
||
78 | |||
79 | /** |
||
80 | * The display plugin id. |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | public $display; |
||
85 | |||
86 | /** |
||
87 | * The display plugin configuration. |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | public $display_configuration = []; |
||
92 | |||
93 | /** |
||
94 | * Display lazy plugin collection. |
||
95 | * |
||
96 | * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
||
97 | */ |
||
98 | protected $displayCollection; |
||
99 | |||
100 | /** |
||
101 | * The array of widgets for this entity browser. |
||
102 | * |
||
103 | * @var array |
||
104 | */ |
||
105 | protected $widgets = []; |
||
106 | |||
107 | /** |
||
108 | * Holds the collection of widgets that are used by this entity browser. |
||
109 | * |
||
110 | * @var \Drupal\entity_browser\WidgetsCollection |
||
111 | */ |
||
112 | protected $widgetsCollection; |
||
113 | |||
114 | /** |
||
115 | * The selection display plugin ID. |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | public $selection_display; |
||
120 | |||
121 | /** |
||
122 | * The selection display plugin configuration. |
||
123 | * |
||
124 | * @var array |
||
125 | */ |
||
126 | public $selection_display_configuration = []; |
||
127 | |||
128 | /** |
||
129 | * Selection display plugin collection. |
||
130 | * |
||
131 | * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
||
132 | */ |
||
133 | protected $selectionDisplayCollection; |
||
134 | |||
135 | /** |
||
136 | * The widget selector plugin ID. |
||
137 | * |
||
138 | * @var string |
||
139 | */ |
||
140 | public $widget_selector; |
||
141 | |||
142 | /** |
||
143 | * The widget selector plugin configuration. |
||
144 | * |
||
145 | * @var array |
||
146 | */ |
||
147 | public $widget_selector_configuration = []; |
||
148 | |||
149 | /** |
||
150 | * Widget selector plugin collection. |
||
151 | * |
||
152 | * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
||
153 | */ |
||
154 | protected $widgetSelectorCollection; |
||
155 | |||
156 | /** |
||
157 | * Additional widget parameters. |
||
158 | * |
||
159 | * @var array |
||
160 | */ |
||
161 | protected $additional_widget_parameters = []; |
||
162 | |||
163 | /** |
||
164 | * Name of the form class. |
||
165 | * |
||
166 | * @var string |
||
167 | */ |
||
168 | protected $form_class = '\Drupal\entity_browser\Form\EntityBrowserForm'; |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | public function id() { |
||
174 | return $this->name; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function getName() { |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | public function setName($name) { |
||
188 | $this->name = $name; |
||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | public function getDisplay() { |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function setLabel($label) { |
||
203 | $this->label = $label; |
||
204 | return $this; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | public function setDisplay($display) { |
||
211 | $this->display = $display; |
||
212 | $this->displayPluginCollection = NULL; |
||
213 | $this->getDisplay(); |
||
214 | return $this; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function setWidgetSelector($widget_selector) { |
||
221 | $this->widget_selector = $widget_selector; |
||
222 | $this->widgetSelectorCollection = NULL; |
||
223 | $this->getWidgetSelector(); |
||
224 | return $this; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function setSelectionDisplay($selection_display) { |
||
231 | $this->selection_display = $selection_display; |
||
232 | $this->selectionDisplayCollection = NULL; |
||
233 | $this->getSelectionDisplay(); |
||
234 | return $this; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Returns display plugin collection. |
||
239 | * |
||
240 | * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
||
241 | * The tag plugin collection. |
||
242 | */ |
||
243 | protected function displayPluginCollection() { |
||
250 | |||
251 | /** |
||
252 | * Returns the plugin collections used by this entity. |
||
253 | * |
||
254 | * @return \Drupal\Component\Plugin\LazyPluginCollection[] |
||
255 | * An array of plugin collections, keyed by the property name they use to |
||
256 | * store their configuration. |
||
257 | */ |
||
258 | public function getPluginCollections() { |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function getWidget($widget) { |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function getWidgets() { |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function addWidget(array $configuration) { |
||
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | */ |
||
300 | public function deleteWidget(WidgetInterface $widget) { |
||
301 | $this->getWidgets()->removeInstanceId($widget->uuid()); |
||
302 | $this->save(); |
||
303 | return $this; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function getFirstWidget() { |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function addAdditionalWidgetParameters(array $parameters) { |
||
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | public function getAdditionalWidgetParameters() { |
||
330 | |||
331 | /** |
||
332 | * Returns selection display plugin collection. |
||
333 | * |
||
334 | * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
||
335 | * The tag plugin collection. |
||
336 | */ |
||
337 | protected function selectionDisplayPluginCollection() { |
||
344 | |||
345 | /** |
||
346 | * {@inheritdoc} |
||
347 | */ |
||
348 | public function getSelectionDisplay() { |
||
351 | |||
352 | /** |
||
353 | * Returns widget selector plugin collection. |
||
354 | * |
||
355 | * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection |
||
356 | * The tag plugin collection. |
||
357 | */ |
||
358 | protected function widgetSelectorPluginCollection() { |
||
369 | |||
370 | /** |
||
371 | * {@inheritdoc} |
||
372 | */ |
||
373 | public function getWidgetSelector() { |
||
374 | return $this->widgetSelectorPluginCollection()->get($this->widget_selector); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | */ |
||
380 | public function route() { |
||
381 | // TODO: Allow displays to define more than just path. |
||
382 | // See: https://www.drupal.org/node/2364193 |
||
383 | $display = $this->getDisplay(); |
||
384 | if ($display instanceof DisplayRouterInterface) { |
||
385 | $path = $display->path(); |
||
386 | return new Route( |
||
387 | $path, |
||
388 | [ |
||
389 | '_controller' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::getContentResult', |
||
390 | '_title_callback' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::title', |
||
391 | 'entity_browser_id' => $this->id(), |
||
392 | ], |
||
393 | ['_permission' => 'access ' . $this->id() . ' entity browser pages'], |
||
394 | ['_admin_route' => \Drupal::config('node.settings')->get('use_admin_theme')] |
||
395 | ); |
||
396 | } |
||
397 | |||
398 | return FALSE; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * {@inheritdoc} |
||
403 | */ |
||
404 | public function preSave(EntityStorageInterface $storage) { |
||
416 | |||
417 | /** |
||
418 | * Prevent plugin collections from being serialized and correctly serialize |
||
419 | * selected entities. |
||
420 | */ |
||
421 | public function __sleep() { |
||
422 | // Save configuration for all plugins. |
||
423 | $this->widgets = $this->getWidgets()->getConfiguration(); |
||
424 | $this->widget_selector_configuration = $this->widgetSelectorPluginCollection()->getConfiguration(); |
||
425 | $this->display_configuration = $this->displayPluginCollection()->getConfiguration(); |
||
426 | $this->selection_display_configuration = $this->selectionDisplayPluginCollection()->getConfiguration(); |
||
427 | |||
428 | return array_diff( |
||
429 | array_keys(get_object_vars($this)), |
||
430 | [ |
||
431 | 'widgetsCollection', |
||
432 | 'widgetSelectorCollection', |
||
433 | 'displayCollection', |
||
434 | 'selectionDisplayCollection', |
||
435 | 'selectedEntities' |
||
436 | ] |
||
437 | ); |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function save() { |
||
450 | |||
451 | /** |
||
452 | * {@inheritdoc} |
||
453 | */ |
||
454 | public function getFormObject() { |
||
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'; |
||
476 | |||
477 | } |
||
478 |