View source
<?php
namespace Drupal\entity_browser\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
use Drupal\entity_browser\EntityBrowserInterface;
use Drupal\entity_browser\WidgetInterface;
use Drupal\entity_browser\DisplayRouterInterface;
use Drupal\entity_browser\WidgetsCollection;
use Symfony\Component\Routing\Route;
class EntityBrowser extends ConfigEntityBase implements EntityBrowserInterface, EntityWithPluginCollectionInterface {
public $name;
public $label;
public $display;
public $display_configuration = [];
protected $displayCollection;
protected $widgets = [];
protected $widgetsCollection;
public $selection_display;
public $selection_display_configuration = [];
protected $selectionDisplayCollection;
public $widget_selector;
public $widget_selector_configuration = [];
protected $widgetSelectorCollection;
protected $additional_widget_parameters = [];
protected $form_class = '\\Drupal\\entity_browser\\Form\\EntityBrowserForm';
public function id() {
return $this->name;
}
public function getName() {
return $this
->get('name');
}
public function setName($name) {
$this->name = $name;
return $this;
}
public function getDisplay() {
return $this
->displayPluginCollection()
->get($this->display);
}
public function setLabel($label) {
$this->label = $label;
return $this;
}
public function setDisplay($display) {
$this->display = $display;
$this->displayPluginCollection = NULL;
$this->display_configuration = [];
$this
->getDisplay();
return $this;
}
public function setWidgetSelector($widget_selector) {
$this->widget_selector = $widget_selector;
$this->widgetSelectorCollection = NULL;
$this->widget_selector_configuration = [];
$this
->getWidgetSelector();
return $this;
}
public function setSelectionDisplay($selection_display) {
$this->selection_display = $selection_display;
$this->selectionDisplayCollection = NULL;
$this->selection_display_configuration = [];
$this
->getSelectionDisplay();
return $this;
}
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;
}
public function getPluginCollections() {
return [
'widgets' => $this
->getWidgets(),
'widget_selector_configuration' => $this
->widgetSelectorPluginCollection(),
'display_configuration' => $this
->displayPluginCollection(),
'selection_display_configuration' => $this
->selectionDisplayPluginCollection(),
];
}
public function getWidget($widget) {
return $this
->getWidgets()
->get($widget);
}
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;
}
public function addWidget(array $configuration) {
$configuration['uuid'] = $this
->uuidGenerator()
->generate();
$this
->getWidgets()
->addInstanceId($configuration['uuid'], $configuration);
return $configuration['uuid'];
}
public function deleteWidget(WidgetInterface $widget) {
$this
->getWidgets()
->removeInstanceId($widget
->uuid());
return $this;
}
public function getFirstWidget() {
$instance_ids = $this
->getWidgets()
->getInstanceIds();
$instance_ids = array_filter($instance_ids, function ($id) {
return $this
->getWidget($id)
->access()
->isAllowed();
});
if (empty($instance_ids)) {
return NULL;
}
return reset($instance_ids);
}
public function addAdditionalWidgetParameters(array $parameters) {
$this->additional_widget_parameters += $parameters;
return $this;
}
public function getAdditionalWidgetParameters() {
return $this
->get('additional_widget_parameters');
}
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;
}
public function getSelectionDisplay() {
return $this
->selectionDisplayPluginCollection()
->get($this->selection_display);
}
protected function widgetSelectorPluginCollection() {
if (!$this->widgetSelectorCollection) {
$options = [];
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;
}
public function getWidgetSelector() {
return $this
->widgetSelectorPluginCollection()
->get($this->widget_selector);
}
public function route() {
$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;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
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']);
}
public function __sleep() {
$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',
]);
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
\Drupal::service('router.builder')
->setRebuildNeeded();
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
\Drupal::service('router.builder')
->setRebuildNeeded();
}
public function getFormObject() {
$form_class = \Drupal::service('class_resolver')
->getInstanceFromDefinition($this->form_class);
$form_class
->setEntityBrowser($this);
return $form_class;
}
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
if ($rel == 'config-translation-overview') {
$uri_route_parameters['step'] = 'general';
}
return $uri_route_parameters;
}
}