Completed
Push — 8.x-1.x ( d4c3a0...0452e7 )
by Janez
03:08
created

DisplayBase::displayEntityBrowser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 9.4285
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
14
/**
15
 * Base implementation for display plugins.
16
 */
17
abstract class DisplayBase extends PluginBase implements DisplayInterface, ContainerFactoryPluginInterface {
18
19
  use PluginConfigurationFormTrait;
20
21
  /**
22
   * Plugin label.
23
   *
24
   * @var string
25
   */
26
  protected $label;
27
28
  /**
29
   * Selected entities.
30
   *
31
   * @var \Drupal\Core\Entity\EntityInterface[]
32
   */
33
  protected $entities = [];
34
35
  /**
36
   * Event dispatcher service.
37
   *
38
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
39
   */
40
  protected $eventDispatcher;
41
42
  /**
43
   * UUID generator interface.
44
   *
45
   * @var \Drupal\Component\Uuid\UuidInterface
46
   */
47
  protected $uuidGenerator;
48
49
  /**
50
   * Instance UUID string.
51
   *
52
   * @var string
53
   */
54
  protected $uuid = NULL;
55
56
  /**
57
   * The selection storage.
58
   *
59
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
60
   */
61
  protected $selectionStorage;
62
63
  /**
64
   * Constructs display plugin.
65
   *
66
   * @param array $configuration
67
   *   A configuration array containing information about the plugin instance.
68
   * @param string $plugin_id
69
   *   The plugin_id for the plugin instance.
70
   * @param mixed $plugin_definition
71
   *   The plugin implementation definition.
72
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
73
   *   Event dispatcher service.
74
   * @param \Drupal\Component\Uuid\UuidInterface
75
   *   UUID generator interface.
76
   */
77
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, UuidInterface $uuid_generator, KeyValueStoreExpirableInterface $selection_storage) {
78
    parent::__construct($configuration, $plugin_id, $plugin_definition);
79
    $this->configuration += $this->defaultConfiguration();
80
    $this->eventDispatcher = $event_dispatcher;
81
    $this->uuidGenerator = $uuid_generator;
82
    $this->selectionStorage = $selection_storage;
83
  }
84
85
  /**
86
   * {@inheritdoc}
87
   */
88
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
89
    return new static(
90
      $configuration,
91
      $plugin_id,
92
      $plugin_definition,
93
      $container->get('event_dispatcher'),
94
      $container->get('uuid'),
95
      $container->get('entity_browser.selection_storage')
96
    );
97
  }
98
99
  /**
100
   * {@inheritdoc}
101
   */
102
  public function defaultConfiguration() {
103
    return [];
104
  }
105
106
  /**
107
   * {@inheritdoc}
108
   */
109
  public function getConfiguration() {
110
    return array_diff_key(
111
      $this->configuration,
112
      ['entity_browser_id' => 0]
113
    );
114
  }
115
116
  /**
117
   * {@inheritdoc}
118
   */
119
  public function setConfiguration(array $configuration) {
120
    $this->configuration = $configuration;
121
  }
122
123
  /**
124
   * {@inheritdoc}
125
   */
126
  public function calculateDependencies() {
127
    return [];
128
  }
129
130
  /**
131
   * {@inheritdoc}
132
   */
133
  public function label() {
134
    return $this->label;
135
  }
136
137
  /**
138
   * {@inheritdoc}
139
   */
140
  public function getUuid() {
141
    if (empty($this->uuid)) {
142
      $this->uuid = $this->uuidGenerator->generate();
143
    }
144
    return $this->uuid;
145
  }
146
147
  /**
148
   * {@inheritdoc}
149
   */
150
  public function setUuid($uuid) {
151
    $this->uuid = $uuid;
152
  }
153
154
  /**
155
   * {@inheritdoc}
156
   */
157
  public function displayEntityBrowser(FormStateInterface $form_state, array $entities = []) {
158
    // If the existing selection was passed in save it to expirable state for
159
    // the entity browser to be able to load them from there.
160
    if (!empty($entities)) {
161
      $this->selectionStorage->setWithExpire($this->getUuid(), $entities, Settings::get('entity_browser_expire', 21600));
162
    }
163
  }
164
165
}
166