SelectionDisplayBase::calculateDependencies()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\entity_browser;
4
5
use Drupal\Component\Utility\NestedArray;
6
use Drupal\Core\Config\ConfigException;
7
use Drupal\Core\Plugin\PluginBase;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\Core\Form\FormStateInterface;
10
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11
use Drupal\entity_browser\Events\Events;
12
use Drupal\entity_browser\Events\SelectionDoneEvent;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
16
/**
17
 * Base implementation for selection display plugins.
18
 */
19
abstract class SelectionDisplayBase extends PluginBase implements SelectionDisplayInterface, ContainerFactoryPluginInterface {
20
21
  use PluginConfigurationFormTrait;
22
23
  /**
24
   * Plugin label.
25
   *
26
   * @var string
27
   */
28
  protected $label;
29
30
  /**
31
   * Event dispatcher service.
32
   *
33
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
34
   */
35
  protected $eventDispatcher;
36
37
  /**
38
   * Entity manager service.
39
   *
40
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
41
   */
42
  protected $entityTypeManager;
43
44
  /**
45
   * Constructs widget plugin.
46
   *
47
   * @param array $configuration
48
   *   A configuration array containing information about the plugin instance.
49
   * @param string $plugin_id
50
   *   The plugin_id for the plugin instance.
51
   * @param mixed $plugin_definition
52
   *   The plugin implementation definition.
53
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
54
   *   Event dispatcher service.
55
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
56
   *   The entity manager type service.
57
   */
58 View Code Duplication
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager) {
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...
59
    parent::__construct($configuration, $plugin_id, $plugin_definition);
60
    $this->eventDispatcher = $event_dispatcher;
61
    $this->entityTypeManager = $entity_type_manager;
62
    $this->setConfiguration($configuration);
63
  }
64
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
69
    return new static(
70
      $configuration,
71
      $plugin_id,
72
      $plugin_definition,
73
      $container->get('event_dispatcher'),
74
      $container->get('entity_type.manager')
75
    );
76
  }
77
78
  /**
79
   * {@inheritdoc}
80
   */
81
  public function defaultConfiguration() {
82
    return [];
83
  }
84
85
  /**
86
   * {@inheritdoc}
87
   */
88
  public function getConfiguration() {
89
    return array_diff_key(
90
      $this->configuration,
91
      ['entity_browser_id' => 0]
92
    );
93
  }
94
95
  /**
96
   * {@inheritdoc}
97
   */
98
  public function setConfiguration(array $configuration) {
99
    $this->configuration = NestedArray::mergeDeep(
100
      $this->defaultConfiguration(),
101
      $configuration
102
    );
103
  }
104
105
  /**
106
   * {@inheritdoc}
107
   */
108
  public function calculateDependencies() {
109
    return [];
110
  }
111
112
  /**
113
   * {@inheritdoc}
114
   */
115
  public function label() {
116
    $this->label;
117
  }
118
119
  /**
120
   * {@inheritdoc}
121
   */
122
  public function validate(array &$form, FormStateInterface $form_state) {}
123
124
  /**
125
   * {@inheritdoc}
126
   */
127
  public function submit(array &$form, FormStateInterface $form_state) {}
128
129
  /**
130
   * {@inheritdoc}
131
   */
132
  public function checkPreselectionSupport() {
133
    if (!$this->getPluginDefinition()['acceptPreselection']) {
134
      throw new ConfigException('Used entity browser selection display does not support preselection.');
135
    }
136
  }
137
138
  /**
139
   * {@inheritdoc}
140
   */
141
  public function supportsJsCommands() {
142
    return $this->getPluginDefinition()['js_commands'];
143
  }
144
145
  /**
146
   * Marks selection as done - sets value in form state and dispatches event.
147
   */
148
  protected function selectionDone(FormStateInterface $form_state) {
149
    $form_state->set(['entity_browser', 'selection_completed'], TRUE);
150
    $this->eventDispatcher->dispatch(
151
      Events::DONE,
152
      new SelectionDoneEvent(
153
        $this->configuration['entity_browser_id'],
154
        $form_state->get(['entity_browser', 'instance_uuid'])
155
      ));
156
  }
157
158
}
159