Completed
Push — 8.x-1.x ( da8d51...96c705 )
by Janez
07:38 queued 03:01
created

DisplayBase::selectionCompleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Drupal\entity_browser;
4
5
use Drupal\Component\Uuid\UuidInterface;
6
use Drupal\Core\Form\FormStateInterface;
7
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
8
use Drupal\Core\Plugin\PluginBase;
9
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10
use Drupal\Core\Site\Settings;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\HttpKernel\KernelEvents;
14
15
/**
16
 * Base implementation for display plugins.
17
 */
18
abstract class DisplayBase extends PluginBase implements DisplayInterface, ContainerFactoryPluginInterface {
19
20
  use PluginConfigurationFormTrait;
21
22
  /**
23
   * Plugin label.
24
   *
25
   * @var string
26
   */
27
  protected $label;
28
29
  /**
30
   * Selected entities.
31
   *
32
   * @var \Drupal\Core\Entity\EntityInterface[]
33
   */
34
  protected $entities = [];
35
36
  /**
37
   * Event dispatcher service.
38
   *
39
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
40
   */
41
  protected $eventDispatcher;
42
43
  /**
44
   * UUID generator interface.
45
   *
46
   * @var \Drupal\Component\Uuid\UuidInterface
47
   */
48
  protected $uuidGenerator;
49
50
  /**
51
   * Instance UUID string.
52
   *
53
   * @var string
54
   */
55
  protected $uuid = NULL;
56
57
  /**
58
   * The selection storage.
59
   *
60
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
61
   */
62
  protected $selectionStorage;
63
64
  /**
65
   * Constructs display plugin.
66
   *
67
   * @param array $configuration
68
   *   A configuration array containing information about the plugin instance.
69
   * @param string $plugin_id
70
   *   The plugin_id for the plugin instance.
71
   * @param mixed $plugin_definition
72
   *   The plugin implementation definition.
73
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
74
   *   Event dispatcher service.
75
   * @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
76
   *   UUID generator interface.
77
   * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $selection_storage
78
   *   The selection storage.
79
   */
80
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, UuidInterface $uuid_generator, KeyValueStoreExpirableInterface $selection_storage) {
81
    parent::__construct($configuration, $plugin_id, $plugin_definition);
82
    $this->configuration += $this->defaultConfiguration();
83
    $this->eventDispatcher = $event_dispatcher;
84
    $this->uuidGenerator = $uuid_generator;
85
    $this->selectionStorage = $selection_storage;
86
  }
87
88
  /**
89
   * {@inheritdoc}
90
   */
91
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
92
    return new static(
93
      $configuration,
94
      $plugin_id,
95
      $plugin_definition,
96
      $container->get('event_dispatcher'),
97
      $container->get('uuid'),
98
      $container->get('entity_browser.selection_storage')
99
    );
100
  }
101
102
  /**
103
   * {@inheritdoc}
104
   */
105
  public function defaultConfiguration() {
106
    return [];
107
  }
108
109
  /**
110
   * {@inheritdoc}
111
   */
112
  public function getConfiguration() {
113
    return array_diff_key(
114
      $this->configuration,
115
      ['entity_browser_id' => 0]
116
    );
117
  }
118
119
  /**
120
   * {@inheritdoc}
121
   */
122
  public function setConfiguration(array $configuration) {
123
    $this->configuration = $configuration;
124
  }
125
126
  /**
127
   * {@inheritdoc}
128
   */
129
  public function calculateDependencies() {
130
    return [];
131
  }
132
133
  /**
134
   * {@inheritdoc}
135
   */
136
  public function label() {
137
    return $this->label;
138
  }
139
140
  /**
141
   * {@inheritdoc}
142
   */
143
  public function getUuid() {
144
    if (empty($this->uuid)) {
145
      $this->uuid = $this->uuidGenerator->generate();
146
    }
147
    return $this->uuid;
148
  }
149
150
  /**
151
   * {@inheritdoc}
152
   */
153
  public function setUuid($uuid) {
154
    $this->uuid = $uuid;
155
  }
156
157
  /**
158
   * {@inheritdoc}
159
   */
160
  public function displayEntityBrowser(array $element, FormStateInterface $form_state, array &$complete_form, array $persistent_data = []) {
161
    // Store persistent data so that after being rendered widgets can still
162
    // have access to contextual information.
163
    $this->selectionStorage->setWithExpire(
164
      $this->getUuid(),
165
      $persistent_data,
166
      Settings::get('entity_browser_expire', 21600)
167
    );
168
  }
169
170
  /**
171
   * {@inheritdoc}
172
   */
173
  public function selectionCompleted(array $entities) {
174
    $this->entities = $entities;
175
    $this->eventDispatcher->addListener(KernelEvents::RESPONSE, [$this, 'propagateSelection']);
176
  }
177
178
}
179