1 | <?php |
||
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() { |
||
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) { |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function getDisplay() { |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function setLabel($label) { |
||
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) { |
||
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() { |
||
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() { |
||
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() { |
||
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', |
||
399 | |||
400 | /** |
||
401 | * {@inheritdoc} |
||
402 | */ |
||
403 | public function preSave(EntityStorageInterface $storage) { |
||
415 | |||
416 | /** |
||
417 | * Prevent plugin collections from being serialized and correctly serialize |
||
418 | * selected entities. |
||
419 | */ |
||
420 | public function __sleep() { |
||
438 | |||
439 | /** |
||
440 | * {@inheritdoc} |
||
441 | */ |
||
442 | public function save() { |
||
449 | |||
450 | /** |
||
451 | * {@inheritdoc} |
||
452 | */ |
||
453 | public function getFormObject() { |
||
458 | |||
459 | |||
460 | /** |
||
461 | * {@inheritdoc} |
||
462 | */ |
||
463 | protected function urlRouteParameters($rel) { |
||
476 | |||
477 | } |
||
478 |