b715e4fa4d0b0f67f7fbad6059ef152f0fea779d
[yaffs-website] / DisplayBase.php
1 <?php
2
3 namespace Drupal\entity_browser;
4
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;
15
16 /**
17  * Base implementation for display plugins.
18  */
19 abstract class DisplayBase extends PluginBase implements DisplayInterface, ContainerFactoryPluginInterface {
20
21   use PluginConfigurationFormTrait;
22
23   /**
24    * Plugin label.
25    *
26    * @var string
27    */
28   protected $label;
29
30   /**
31    * Selected entities.
32    *
33    * @var \Drupal\Core\Entity\EntityInterface[]
34    */
35   protected $entities = [];
36
37   /**
38    * Event dispatcher service.
39    *
40    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
41    */
42   protected $eventDispatcher;
43
44   /**
45    * UUID generator interface.
46    *
47    * @var \Drupal\Component\Uuid\UuidInterface
48    */
49   protected $uuidGenerator;
50
51   /**
52    * Instance UUID string.
53    *
54    * @var string
55    */
56   protected $uuid = NULL;
57
58   /**
59    * The selection storage.
60    *
61    * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
62    */
63   protected $selectionStorage;
64
65   /**
66    * Constructs display plugin.
67    *
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.
80    */
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;
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
93     return new static(
94       $configuration,
95       $plugin_id,
96       $plugin_definition,
97       $container->get('event_dispatcher'),
98       $container->get('uuid'),
99       $container->get('entity_browser.selection_storage')
100     );
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function defaultConfiguration() {
107     return [];
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function getConfiguration() {
114     return array_diff_key(
115       $this->configuration,
116       ['entity_browser_id' => 0]
117     );
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   public function setConfiguration(array $configuration) {
124     $this->configuration = NestedArray::mergeDeep(
125       $this->defaultConfiguration(),
126       $configuration
127     );
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function calculateDependencies() {
134     return [];
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function label() {
141     return $this->label;
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function getUuid() {
148     if (empty($this->uuid)) {
149       $this->uuid = $this->uuidGenerator->generate();
150     }
151     return $this->uuid;
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public function setUuid($uuid) {
158     $this->uuid = $uuid;
159   }
160
161   /**
162    * {@inheritdoc}
163    */
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(
168       $this->getUuid(),
169       $persistent_data,
170       Settings::get('entity_browser_expire', 21600)
171     );
172   }
173
174   /**
175    * {@inheritdoc}
176    */
177   public function selectionCompleted(array $entities) {
178     $this->entities = $entities;
179     $this->eventDispatcher->addListener(KernelEvents::RESPONSE, [$this, 'propagateSelection']);
180   }
181
182 }