X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fentity_browser%2Fsrc%2FEntity%2FEntityBrowser.php;fp=web%2Fmodules%2Fcontrib%2Fentity_browser%2Fsrc%2FEntity%2FEntityBrowser.php;h=6011f632abd56ed67b9990beb9b3351bc4159b98;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/entity_browser/src/Entity/EntityBrowser.php b/web/modules/contrib/entity_browser/src/Entity/EntityBrowser.php new file mode 100644 index 000000000..6011f632a --- /dev/null +++ b/web/modules/contrib/entity_browser/src/Entity/EntityBrowser.php @@ -0,0 +1,468 @@ +name; + } + + /** + * {@inheritdoc} + */ + public function getName() { + return $this->get('name'); + } + + /** + * {@inheritdoc} + */ + public function setName($name) { + $this->name = $name; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getDisplay() { + return $this->displayPluginCollection()->get($this->display); + } + + /** + * {@inheritdoc} + */ + public function setLabel($label) { + $this->label = $label; + return $this; + } + + /** + * {@inheritdoc} + */ + public function setDisplay($display) { + $this->display = $display; + $this->displayPluginCollection = NULL; + $this->getDisplay(); + return $this; + } + + /** + * {@inheritdoc} + */ + public function setWidgetSelector($widget_selector) { + $this->widget_selector = $widget_selector; + $this->widgetSelectorCollection = NULL; + $this->getWidgetSelector(); + return $this; + } + + /** + * {@inheritdoc} + */ + public function setSelectionDisplay($selection_display) { + $this->selection_display = $selection_display; + $this->selectionDisplayCollection = NULL; + $this->getSelectionDisplay(); + return $this; + } + + /** + * Returns display plugin collection. + * + * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection + * The tag plugin collection. + */ + protected function displayPluginCollection() { + if (!$this->displayCollection) { + $this->display_configuration['entity_browser_id'] = $this->id(); + $this->displayCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.display'), $this->display, $this->display_configuration); + } + return $this->displayCollection; + } + + /** + * Returns the plugin collections used by this entity. + * + * @return \Drupal\Component\Plugin\LazyPluginCollection[] + * An array of plugin collections, keyed by the property name they use to + * store their configuration. + */ + public function getPluginCollections() { + return [ + 'widgets' => $this->getWidgets(), + 'widget_selector_configuration' => $this->widgetSelectorPluginCollection(), + 'display_configuration' => $this->displayPluginCollection(), + 'selection_display_configuration' => $this->selectionDisplayPluginCollection(), + ]; + } + + /** + * {@inheritdoc} + */ + public function getWidget($widget) { + return $this->getWidgets()->get($widget); + } + + /** + * {@inheritdoc} + */ + public function getWidgets() { + if (!$this->widgetsCollection) { + foreach ($this->widgets as &$widget) { + $widget['settings']['entity_browser_id'] = $this->id(); + } + $this->widgetsCollection = new WidgetsCollection(\Drupal::service('plugin.manager.entity_browser.widget'), $this->widgets); + $this->widgetsCollection->sort(); + } + return $this->widgetsCollection; + } + + /** + * {@inheritdoc} + */ + public function addWidget(array $configuration) { + $configuration['uuid'] = $this->uuidGenerator()->generate(); + $this->getWidgets()->addInstanceId($configuration['uuid'], $configuration); + return $configuration['uuid']; + } + + /** + * {@inheritdoc} + */ + public function deleteWidget(WidgetInterface $widget) { + $this->getWidgets()->removeInstanceId($widget->uuid()); + $this->save(); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getFirstWidget() { + $instance_ids = $this->getWidgets()->getInstanceIds(); + return reset($instance_ids); + } + + /** + * {@inheritdoc} + */ + public function addAdditionalWidgetParameters(array $parameters) { + // TODO - this doesn't make much sense. Refactor. + $this->additional_widget_parameters += $parameters; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getAdditionalWidgetParameters() { + // TODO - this doesn't make much sense. Refactor. + return $this->get('additional_widget_parameters'); + } + + /** + * Returns selection display plugin collection. + * + * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection + * The tag plugin collection. + */ + protected function selectionDisplayPluginCollection() { + if (!$this->selectionDisplayCollection) { + $this->selection_display_configuration['entity_browser_id'] = $this->id(); + $this->selectionDisplayCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.selection_display'), $this->selection_display, $this->selection_display_configuration); + } + return $this->selectionDisplayCollection; + } + + /** + * {@inheritdoc} + */ + public function getSelectionDisplay() { + return $this->selectionDisplayPluginCollection()->get($this->selection_display); + } + + /** + * Returns widget selector plugin collection. + * + * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection + * The tag plugin collection. + */ + protected function widgetSelectorPluginCollection() { + if (!$this->widgetSelectorCollection) { + $options = array(); + foreach ($this->getWidgets()->getInstanceIds() as $id) { + $options[$id] = $this->getWidgets()->get($id)->label(); + } + $this->widget_selector_configuration['widget_ids'] = $options; + $this->widgetSelectorCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.entity_browser.widget_selector'), $this->widget_selector, $this->widget_selector_configuration); + } + return $this->widgetSelectorCollection; + } + + /** + * {@inheritdoc} + */ + public function getWidgetSelector() { + return $this->widgetSelectorPluginCollection()->get($this->widget_selector); + } + + /** + * {@inheritdoc} + */ + public function route() { + // TODO: Allow displays to define more than just path. + // See: https://www.drupal.org/node/2364193 + $display = $this->getDisplay(); + if ($display instanceof DisplayRouterInterface) { + $path = $display->path(); + return new Route( + $path, + [ + '_controller' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::getContentResult', + '_title_callback' => 'Drupal\entity_browser\Controllers\EntityBrowserFormController::title', + 'entity_browser_id' => $this->id(), + ], + ['_permission' => 'access ' . $this->id() . ' entity browser pages'], + ['_admin_route' => \Drupal::config('node.settings')->get('use_admin_theme')] + ); + } + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function preSave(EntityStorageInterface $storage) { + parent::preSave($storage); + + // Entity browser ID was added when creating. No need to save that as it can + // always be calculated. + foreach ($this->widgets as &$widget) { + unset($widget['settings']['entity_browser_id']); + } + unset($this->selection_display_configuration['entity_browser_id']); + unset($this->display_configuration['entity_browser_id']); + unset($this->widget_selector_configuration['widget_ids']); + } + + /** + * Prevent plugin collections from being serialized and correctly serialize + * selected entities. + */ + public function __sleep() { + // Save configuration for all plugins. + $this->widgets = $this->getWidgets()->getConfiguration(); + $this->widget_selector_configuration = $this->widgetSelectorPluginCollection()->getConfiguration(); + $this->display_configuration = $this->displayPluginCollection()->getConfiguration(); + $this->selection_display_configuration = $this->selectionDisplayPluginCollection()->getConfiguration(); + + return array_diff( + array_keys(get_object_vars($this)), + [ + 'widgetsCollection', + 'widgetSelectorCollection', + 'displayCollection', + 'selectionDisplayCollection', + 'selectedEntities', + ] + ); + } + + /** + * {@inheritdoc} + */ + public function save() { + $return = parent::save(); + // Rebuild route information when browsers that register routes + // are created/updated. + \Drupal::service('router.builder')->rebuild(); + return $return; + } + + /** + * {@inheritdoc} + */ + public function getFormObject() { + $form_class = \Drupal::service('class_resolver')->getInstanceFromDefinition($this->form_class); + $form_class->setEntityBrowser($this); + return $form_class; + } + + /** + * {@inheritdoc} + */ + protected function urlRouteParameters($rel) { + $uri_route_parameters = parent::urlRouteParameters($rel); + + if ($rel == 'config-translation-overview') { + $uri_route_parameters['step'] = 'general'; + } + + return $uri_route_parameters; + } + +}