3 namespace Drupal\entity_browser;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Component\Uuid\UuidInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
9 use Drupal\Core\Plugin\PluginBase;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Drupal\Core\Site\Settings;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14 use Symfony\Component\HttpKernel\KernelEvents;
17 * Base implementation for display plugins.
19 abstract class DisplayBase extends PluginBase implements DisplayInterface, ContainerFactoryPluginInterface {
21 use PluginConfigurationFormTrait;
33 * @var \Drupal\Core\Entity\EntityInterface[]
35 protected $entities = [];
38 * Event dispatcher service.
40 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
42 protected $eventDispatcher;
45 * UUID generator interface.
47 * @var \Drupal\Component\Uuid\UuidInterface
49 protected $uuidGenerator;
52 * Instance UUID string.
56 protected $uuid = NULL;
59 * The selection storage.
61 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
63 protected $selectionStorage;
66 * Constructs display plugin.
68 * @param array $configuration
69 * A configuration array containing information about the plugin instance.
70 * @param string $plugin_id
71 * The plugin_id for the plugin instance.
72 * @param mixed $plugin_definition
73 * The plugin implementation definition.
74 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
75 * Event dispatcher service.
76 * @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
77 * UUID generator interface.
78 * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $selection_storage
79 * The selection storage.
81 public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, UuidInterface $uuid_generator, KeyValueStoreExpirableInterface $selection_storage) {
82 parent::__construct($configuration, $plugin_id, $plugin_definition);
83 $this->setConfiguration($configuration);
84 $this->eventDispatcher = $event_dispatcher;
85 $this->uuidGenerator = $uuid_generator;
86 $this->selectionStorage = $selection_storage;
92 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
97 $container->get('event_dispatcher'),
98 $container->get('uuid'),
99 $container->get('entity_browser.selection_storage')
106 public function defaultConfiguration() {
113 public function getConfiguration() {
114 return array_diff_key(
115 $this->configuration,
116 ['entity_browser_id' => 0]
123 public function setConfiguration(array $configuration) {
124 $this->configuration = NestedArray::mergeDeep(
125 $this->defaultConfiguration(),
133 public function calculateDependencies() {
140 public function label() {
147 public function getUuid() {
148 if (empty($this->uuid)) {
149 $this->uuid = $this->uuidGenerator->generate();
157 public function setUuid($uuid) {
164 public function displayEntityBrowser(array $element, FormStateInterface $form_state, array &$complete_form, array $persistent_data = []) {
165 // Store persistent data so that after being rendered widgets can still
166 // have access to contextual information.
167 $this->selectionStorage->setWithExpire(
170 Settings::get('entity_browser_expire', 21600)
177 public function selectionCompleted(array $entities) {
178 $this->entities = $entities;
179 $this->eventDispatcher->addListener(KernelEvents::RESPONSE, [$this, 'propagateSelection']);