Completed
Pull Request — 8.x-1.x (#117)
by
unknown
79:15 queued 60:33
created

WidgetBase::runWidgetValidators()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 8
nc 6
nop 2
1
<?php
2
3
/**
4
 * Contains \Drupal\entity_browser\WidgetBase.
5
 */
6
7
namespace Drupal\entity_browser;
8
9
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
10
use Drupal\Core\Plugin\PluginBase;
11
use Drupal\Core\Entity\EntityManagerInterface;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14
use Drupal\entity_browser\Events\EntitySelectionEvent;
15
use Drupal\entity_browser\Events\Events;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * Base class for widget plugins.
22
 */
23
abstract class WidgetBase extends PluginBase implements WidgetInterface, ContainerFactoryPluginInterface {
24
25
  /**
26
   * Plugin id.
27
   *
28
   * @var string
29
   */
30
  protected $id;
31
32
  /**
33
   * Plugin uuid.
34
   *
35
   * @var string
36
   */
37
  protected $uuid;
38
  /**
39
   * Plugin label.
40
   *
41
   * @var string
42
   */
43
  protected $label;
44
45
  /**
46
   * Plugin weight.
47
   *
48
   * @var int
49
   */
50
  protected $weight;
51
52
  /**
53
   * Event dispatcher service.
54
   *
55
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
56
   */
57
  protected $eventDispatcher;
58
59
  /**
60
   * Entity manager service.
61
   *
62
   * @var \Drupal\Core\Entity\EntityManagerInterface
63
   */
64
  protected $entityManager;
65
  /**
66
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
67
   */
68
  private $keyValue;
69
  /**
70
   * @var \Symfony\Component\HttpFoundation\Request
71
   */
72
  private $request;
73
74
  /**
75
   * Constructs widget plugin.
76
   *
77
   * @param array $configuration
78
   *   A configuration array containing information about the plugin instance.
79
   * @param string $plugin_id
80
   *   The plugin_id for the plugin instance.
81
   * @param mixed $plugin_definition
82
   *   The plugin implementation definition.
83
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
84
   *   Event dispatcher service.
85
   */
86 View Code Duplication
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityManagerInterface $entity_manager, KeyValueStoreExpirableInterface $key_value, Request $request) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    parent::__construct($configuration, $plugin_id, $plugin_definition);
88
    $this->eventDispatcher = $event_dispatcher;
89
    $this->entityManager = $entity_manager;
90
    $this->setConfiguration($configuration);
91
    $this->keyValue = $key_value;
92
    $this->request = $request;
93
  }
94
95
  /**
96
   * {@inheritdoc}
97
   */
98
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
99
    return new static(
100
      $configuration,
101
      $plugin_id,
102
      $plugin_definition,
103
      $container->get('event_dispatcher'),
104
      $container->get('entity.manager'),
105
      $container->get('keyvalue.expirable')->get('entity_browser'),
106
      $container->get('request_stack')->getCurrentRequest()
107
    );
108
  }
109
110
  /**
111
   * {@inheritdoc}
112
   */
113
  public function defaultConfiguration() {
114
    return [];
115
  }
116
117
  /**
118
   * {@inheritdoc}
119
   */
120
  public function getConfiguration() {
121
    return [
122
      'settings' => $this->configuration,
123
      'uuid' => $this->uuid(),
124
      'weight' => $this->getWeight(),
125
      'label' => $this->label(),
126
      'id' => $this->id(),
127
    ];
128
  }
129
130
  /**
131
   * {@inheritdoc}
132
   */
133
  public function setConfiguration(array $configuration) {
134
    $configuration += [
135
      'settings' => [],
136
      'uuid' => '',
137
      'weight' => '',
138
      'label' => '',
139
      'id' => '',
140
    ];
141
142
    $this->configuration = $configuration['settings'] + $this->defaultConfiguration();
143
    $this->label = $configuration['label'];
144
    $this->weight = $configuration['weight'];
145
    $this->uuid = $configuration['uuid'];
146
    $this->id = $configuration['id'];
147
  }
148
149
  /**
150
   * {@inheritdoc}
151
   */
152
  public function calculateDependencies() {
153
    return [];
154
  }
155
156
  /**
157
   * {@inheritdoc}
158
   */
159
  public function id() {
160
    return $this->id;
161
  }
162
163
  /**
164
   * {@inheritdoc}
165
   */
166
  public function uuid() {
167
    return $this->uuid;
168
  }
169
170
  /**
171
   * {@inheritdoc}
172
   */
173
  public function label() {
174
    return $this->label;
175
  }
176
177
  /**
178
   * {@inheritdoc}
179
   */
180
  public function getWeight() {
181
    return $this->weight;
182
  }
183
184
  /**
185
   * {@inheritdoc}
186
   */
187
  public function setWeight($weight) {
188
    $this->weight = $weight;
189
    return $this;
190
  }
191
192
  /**
193
   * {@inheritdoc}
194
   */
195
  public function validate(array &$form, FormStateInterface $form_state) {}
196
197
198
  /**
199
   * {@inheritdoc}
200
   */
201
  public function runWidgetValidators(array $entities, $validators = []) {
202
    // @todo Implement a centralized way to get arguments from path for all widgets?
203
    if ($validators_hash = $this->request->get('validators')) {
204
      $passed_validators = $this->keyValue->get($validators_hash);
205
206
      if (!empty($passed_validators)) {
207
        $validators += $passed_validators;
208
      }
209
    }
210
211
    foreach ($validators as $validator_id => $options) {
212
      /** @var \Drupal\entity_browser\WidgetValidationInterface $widget_validator */
213
      $widget_validator = \Drupal::service('plugin.manager.entity_browser.widget_validation')->createInstance($validator_id, []);
214
      return $widget_validator->validate($entities, $options);
215
    }
216
  }
217
218
  /**
219
   * {@inheritdoc}
220
   */
221
  public function submit(array &$element, array &$form, FormStateInterface $form_state) {}
222
223
  /**
224
   * Dispatches event that informs all subscribers about new selected entities.
225
   *
226
   * @param array $entities
227
   *   Array of entities.
228
   */
229
  protected function selectEntities(array $entities, FormStateInterface $form_state) {
230
    $selected_entities = &$form_state->get(['entity_browser', 'selected_entities']);
231
    $selected_entities = array_merge($selected_entities, $entities);
232
233
    $this->eventDispatcher->dispatch(
234
      Events::SELECTED,
235
      new EntitySelectionEvent(
236
        $this->configuration['entity_browser_id'],
237
        $form_state->get(['entity_browser', 'instance_uuid']),
238
        $entities
239
      ));
240
  }
241
242
}
243