View source
<?php
namespace Drupal\entity_browser;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Site\Settings;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\KernelEvents;
abstract class DisplayBase extends PluginBase implements DisplayInterface, ContainerFactoryPluginInterface {
use PluginConfigurationFormTrait;
protected $label;
protected $entities = [];
protected $eventDispatcher;
protected $uuidGenerator;
protected $uuid = NULL;
protected $selectionStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, UuidInterface $uuid_generator, KeyValueStoreExpirableInterface $selection_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
$this->eventDispatcher = $event_dispatcher;
$this->uuidGenerator = $uuid_generator;
$this->selectionStorage = $selection_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('event_dispatcher'), $container
->get('uuid'), $container
->get('entity_browser.selection_storage'));
}
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return array_diff_key($this->configuration, [
'entity_browser_id' => 0,
]);
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
public function calculateDependencies() {
return [];
}
public function label() {
return $this->label;
}
public function getUuid() {
if (empty($this->uuid)) {
$this->uuid = $this->uuidGenerator
->generate();
}
return $this->uuid;
}
public function setUuid($uuid) {
$this->uuid = $uuid;
}
public function displayEntityBrowser(array $element, FormStateInterface $form_state, array &$complete_form, array $persistent_data = []) {
$this->selectionStorage
->setWithExpire($this
->getUuid(), $persistent_data, Settings::get('entity_browser_expire', 21600));
}
public function selectionCompleted(array $entities) {
$this->entities = $entities;
$this->eventDispatcher
->addListener(KernelEvents::RESPONSE, [
$this,
'propagateSelection',
]);
}
}