Completed
Pull Request — 8.x-1.x (#117)
by
unknown
02:48
created

WidgetBase::uuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Contains \Drupal\entity_browser\WidgetBase.
5
 */
6
7
namespace Drupal\entity_browser;
8
9
use Drupal\Core\Plugin\PluginBase;
10
use Drupal\Core\Entity\EntityManagerInterface;
11
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
13
use Drupal\entity_browser\Events\EntitySelectionEvent;
14
use Drupal\entity_browser\Events\Events;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
18
/**
19
 * Base class for widget plugins.
20
 */
21
abstract class WidgetBase extends PluginBase implements WidgetInterface, ContainerFactoryPluginInterface {
22
23
  /**
24
   * Plugin id.
25
   *
26
   * @var string
27
   */
28
  protected $id;
29
30
  /**
31
   * Plugin uuid.
32
   *
33
   * @var string
34
   */
35
  protected $uuid;
36
  /**
37
   * Plugin label.
38
   *
39
   * @var string
40
   */
41
  protected $label;
42
43
  /**
44
   * Plugin weight.
45
   *
46
   * @var int
47
   */
48
  protected $weight;
49
50
  /**
51
   * Event dispatcher service.
52
   *
53
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
54
   */
55
  protected $eventDispatcher;
56
57
  /**
58
   * Entity manager service.
59
   *
60
   * @var \Drupal\Core\Entity\EntityManagerInterface
61
   */
62
  protected $entityManager;
63
64
  /**
65
   * Constructs widget 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
   */
76
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityManagerInterface $entity_manager) {
77
    parent::__construct($configuration, $plugin_id, $plugin_definition);
78
    $this->eventDispatcher = $event_dispatcher;
79
    $this->entityManager = $entity_manager;
80
    $this->setConfiguration($configuration);
81
  }
82
83
  /**
84
   * {@inheritdoc}
85
   */
86
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
87
    return new static(
88
      $configuration,
89
      $plugin_id,
90
      $plugin_definition,
91
      $container->get('event_dispatcher'),
92
      $container->get('entity.manager')
93
    );
94
  }
95
96
  /**
97
   * {@inheritdoc}
98
   */
99
  public function defaultConfiguration() {
100
    return [];
101
  }
102
103
  /**
104
   * {@inheritdoc}
105
   */
106
  public function getConfiguration() {
107
    return [
108
      'settings' => $this->configuration,
109
      'uuid' => $this->uuid(),
110
      'weight' => $this->getWeight(),
111
      'label' => $this->label(),
112
      'id' => $this->id(),
113
    ];
114
  }
115
116
  /**
117
   * {@inheritdoc}
118
   */
119
  public function setConfiguration(array $configuration) {
120
    $configuration += [
121
      'settings' => [],
122
      'uuid' => '',
123
      'weight' => '',
124
      'label' => '',
125
      'id' => '',
126
    ];
127
128
    $this->configuration = $configuration['settings'] + $this->defaultConfiguration();
129
    $this->label = $configuration['label'];
130
    $this->weight = $configuration['weight'];
131
    $this->uuid = $configuration['uuid'];
132
    $this->id = $configuration['id'];
133
  }
134
135
  /**
136
   * {@inheritdoc}
137
   */
138
  public function calculateDependencies() {
139
    return [];
140
  }
141
142
  /**
143
   * {@inheritdoc}
144
   */
145
  public function id() {
146
    return $this->id;
147
  }
148
149
  /**
150
   * {@inheritdoc}
151
   */
152
  public function uuid() {
153
    return $this->uuid;
154
  }
155
156
  /**
157
   * {@inheritdoc}
158
   */
159
  public function label() {
160
    return $this->label;
161
  }
162
163
  /**
164
   * {@inheritdoc}
165
   */
166
  public function getWeight() {
167
    return $this->weight;
168
  }
169
170
  /**
171
   * {@inheritdoc}
172
   */
173
  public function setWeight($weight) {
174
    $this->weight = $weight;
175
    return $this;
176
  }
177
178
  /**
179
   * {@inheritdoc}
180
   */
181
  public function validate(array &$form, FormStateInterface $form_state) {}
182
183
184
  /**
185
   * {@inheritdoc}
186
   */
187
  public function runWidgetValidators(array $entities, $validators = []) {
188
    // @todo Get the list of validation ids.
189
    $all_validators = $validators + [];
0 ignored issues
show
Unused Code introduced by
$all_validators is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
190
191
    foreach ($validators as $validator_id => $options) {
192
      /** @var \Drupal\entity_browser\WidgetValidationInterface $widget_validator */
193
      $widget_validator = \Drupal::service('plugin.manager.entity_browser.widget_validation')->createInstance($validator_id, []);
194
      return $widget_validator->validate($entities, $options);
195
    }
196
  }
197
198
  /**
199
   * {@inheritdoc}
200
   */
201
  public function submit(array &$element, array &$form, FormStateInterface $form_state) {}
202
203
  /**
204
   * Dispatches event that informs all subscribers about new selected entities.
205
   *
206
   * @param array $entities
207
   *   Array of entities.
208
   */
209
  protected function selectEntities(array $entities, FormStateInterface $form_state) {
210
    $selected_entities = &$form_state->get(['entity_browser', 'selected_entities']);
211
    $selected_entities = array_merge($selected_entities, $entities);
212
213
    $this->eventDispatcher->dispatch(
214
      Events::SELECTED,
215
      new EntitySelectionEvent(
216
        $this->configuration['entity_browser_id'],
217
        $form_state->get(['entity_browser', 'instance_uuid']),
218
        $entities
219
      ));
220
  }
221
}
222