3 namespace Drupal\entity_browser\Entity;
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
8 use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
9 use Drupal\entity_browser\EntityBrowserInterface;
10 use Drupal\entity_browser\WidgetInterface;
11 use Drupal\entity_browser\DisplayRouterInterface;
12 use Drupal\entity_browser\WidgetsCollection;
13 use Symfony\Component\Routing\Route;
16 * Defines an entity browser configuration entity.
19 * id = "entity_browser",
20 * label = @Translation("Entity browser"),
23 * "entity_browser" = "Drupal\entity_browser\Form\EntityBrowserForm",
24 * "delete" = "Drupal\entity_browser\Form\EntityBrowserDeleteForm",
26 * "access" = "Drupal\Core\Entity\EntityAccessControlHandler",
27 * "list_builder" = "Drupal\entity_browser\Controllers\EntityBrowserListBuilder",
29 * "add" = "Drupal\entity_browser\Wizard\EntityBrowserWizardAdd",
30 * "edit" = "Drupal\entity_browser\Wizard\EntityBrowserWizard",
34 * "canonical" = "/admin/config/content/entity_browser/{machine_name}/{step}",
35 * "collection" = "/admin/config/content/entity_browser",
36 * "edit-form" = "/admin/config/content/entity_browser/{machine_name}/{step}",
37 * "delete-form" = "/admin/config/content/entity_browser/{entity_browser}/delete",
39 * admin_permission = "administer entity browsers",
40 * config_prefix = "browser",
49 * "display_configuration",
50 * "selection_display",
51 * "selection_display_configuration",
53 * "widget_selector_configuration",
58 class EntityBrowser extends ConfigEntityBase implements EntityBrowserInterface, EntityWithPluginCollectionInterface {
61 * The name of the entity browser.
68 * The entity browser label.
75 * The display plugin id.
82 * The display plugin configuration.
86 public $display_configuration = [];
89 * Display lazy plugin collection.
91 * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
93 protected $displayCollection;
96 * The array of widgets for this entity browser.
100 protected $widgets = [];
103 * Holds the collection of widgets that are used by this entity browser.
105 * @var \Drupal\entity_browser\WidgetsCollection
107 protected $widgetsCollection;
110 * The selection display plugin ID.
114 public $selection_display;
117 * The selection display plugin configuration.
121 public $selection_display_configuration = [];
124 * Selection display plugin collection.
126 * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
128 protected $selectionDisplayCollection;
131 * The widget selector plugin ID.
135 public $widget_selector;
138 * The widget selector plugin configuration.
142 public $widget_selector_configuration = [];
145 * Widget selector plugin collection.
147 * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
149 protected $widgetSelectorCollection;
152 * Additional widget parameters.
156 protected $additional_widget_parameters = [];
159 * Name of the form class.
163 protected $form_class = '\Drupal\entity_browser\Form\EntityBrowserForm';
168 public function id() {
175 public function getName() {
176 return $this->get('name');
182 public function setName($name) {
190 public function getDisplay() {
191 return $this->displayPluginCollection()->get($this->display);
197 public function setLabel($label) {
198 $this->label = $label;
205 public function setDisplay($display) {
206 $this->display = $display;
207 $this->displayPluginCollection = NULL;
215 public function setWidgetSelector($widget_selector) {
216 $this->widget_selector = $widget_selector;
217 $this->widgetSelectorCollection = NULL;
218 $this->getWidgetSelector();
225 public function setSelectionDisplay($selection_display) {
226 $this->selection_display = $selection_display;
227 $this->selectionDisplayCollection = NULL;
228 $this->getSelectionDisplay();
233 * Returns display plugin collection.
235 * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
236 * The tag plugin collection.
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);
243 return $this->displayCollection;
247 * Returns the plugin collections used by this entity.
249 * @return \Drupal\Component\Plugin\LazyPluginCollection[]
250 * An array of plugin collections, keyed by the property name they use to
251 * store their configuration.
253 public function getPluginCollections() {
255 'widgets' => $this->getWidgets(),
256 'widget_selector_configuration' => $this->widgetSelectorPluginCollection(),
257 'display_configuration' => $this->displayPluginCollection(),
258 'selection_display_configuration' => $this->selectionDisplayPluginCollection(),
265 public function getWidget($widget) {
266 return $this->getWidgets()->get($widget);
272 public function getWidgets() {
273 if (!$this->widgetsCollection) {
274 foreach ($this->widgets as &$widget) {
275 $widget['settings']['entity_browser_id'] = $this->id();
277 $this->widgetsCollection = new WidgetsCollection(\Drupal::service('plugin.manager.entity_browser.widget'), $this->widgets);
278 $this->widgetsCollection->sort();
280 return $this->widgetsCollection;
286 public function addWidget(array $configuration) {
287 $configuration['uuid'] = $this->uuidGenerator()->generate();
288 $this->getWidgets()->addInstanceId($configuration['uuid'], $configuration);
289 return $configuration['uuid'];
295 public function deleteWidget(WidgetInterface $widget) {
296 $this->getWidgets()->removeInstanceId($widget->uuid());
304 public function getFirstWidget() {
305 $instance_ids = $this->getWidgets()->getInstanceIds();
306 return reset($instance_ids);
312 public function addAdditionalWidgetParameters(array $parameters) {
313 // TODO - this doesn't make much sense. Refactor.
314 $this->additional_widget_parameters += $parameters;
321 public function getAdditionalWidgetParameters() {
322 // TODO - this doesn't make much sense. Refactor.
323 return $this->get('additional_widget_parameters');
327 * Returns selection display plugin collection.
329 * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
330 * The tag plugin collection.
332 protected function selectionDisplayPluginCollection() {
333 if (!$this->selectionDisplayCollection) {
334 $this->selection_display_configuration['entity_browser_id'] = $this->id();
335 $this->selectionDisplayCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.selection_display'), $this->selection_display, $this->selection_display_configuration);
337 return $this->selectionDisplayCollection;
343 public function getSelectionDisplay() {
344 return $this->selectionDisplayPluginCollection()->get($this->selection_display);
348 * Returns widget selector plugin collection.
350 * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
351 * The tag plugin collection.
353 protected function widgetSelectorPluginCollection() {
354 if (!$this->widgetSelectorCollection) {
356 foreach ($this->getWidgets()->getInstanceIds() as $id) {
357 $options[$id] = $this->getWidgets()->get($id)->label();
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);
362 return $this->widgetSelectorCollection;
368 public function getWidgetSelector() {
369 return $this->widgetSelectorPluginCollection()->get($this->widget_selector);
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();
384 '_controller' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::getContentResult',
385 '_title_callback' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::title',
386 'entity_browser_id' => $this->id(),
388 ['_permission' => 'access ' . $this->id() . ' entity browser pages'],
389 ['_admin_route' => \Drupal::config('node.settings')->get('use_admin_theme')]
399 public function preSave(EntityStorageInterface $storage) {
400 parent::preSave($storage);
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']);
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']);
413 * Prevent plugin collections from being serialized and correctly serialize
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();
424 array_keys(get_object_vars($this)),
427 'widgetSelectorCollection',
429 'selectionDisplayCollection',
438 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
439 parent::postSave($storage, $update);
440 // Rebuild route information when browsers that register routes
441 // are created/updated.
442 \Drupal::service('router.builder')->setRebuildNeeded();
448 public static function postDelete(EntityStorageInterface $storage, array $entities) {
449 parent::postDelete($storage, $entities);
450 // Rebuild route information when browsers that register routes
452 \Drupal::service('router.builder')->setRebuildNeeded();
458 public function getFormObject() {
459 $form_class = \Drupal::service('class_resolver')->getInstanceFromDefinition($this->form_class);
460 $form_class->setEntityBrowser($this);
467 protected function urlRouteParameters($rel) {
468 $uri_route_parameters = parent::urlRouteParameters($rel);
470 if ($rel == 'config-translation-overview') {
471 $uri_route_parameters['step'] = 'general';
474 return $uri_route_parameters;