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