1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Contains \Drupal\entity_browser\Form\WidgetsConfig. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Drupal\entity_browser\Form; |
9
|
|
|
|
10
|
|
|
use Drupal\Core\Form\FormBase; |
11
|
|
|
use Drupal\Core\Form\FormStateInterface; |
12
|
|
|
use Drupal\entity_browser\WidgetManager; |
13
|
|
|
use Drupal\user\SharedTempStoreFactory; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
15
|
|
|
|
16
|
|
|
class WidgetsConfig extends FormBase { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Entity browser widget plugin manager. |
20
|
|
|
* |
21
|
|
|
* @var \Drupal\entity_browser\WidgetManager |
22
|
|
|
*/ |
23
|
|
|
protected $widgetManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Tempstore Factory for keeping track of values in each step of the wizard. |
27
|
|
|
* |
28
|
|
|
* @var \Drupal\user\SharedTempStoreFactory |
29
|
|
|
*/ |
30
|
|
|
protected $tempStore; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* WidgetsConfig constructor. |
34
|
|
|
* @param \Drupal\entity_browser\WidgetManager $widget_manager |
35
|
|
|
* @param \Drupal\user\SharedTempStoreFactory $temp_store |
36
|
|
|
*/ |
37
|
|
|
function __construct(WidgetManager $widget_manager, SharedTempStoreFactory $temp_store) { |
|
|
|
|
38
|
|
|
$this->widgetManager = $widget_manager; |
39
|
|
|
$this->tempStore = $temp_store; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public static function create(ContainerInterface $container) { |
46
|
|
|
return new static( |
47
|
|
|
$container->get('plugin.manager.entity_browser.widget'), |
48
|
|
|
$container->get('user.shared_tempstore') |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function getFormId() { |
56
|
|
|
return 'entity_browser_widgets_config_form'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function buildForm(array $form, FormStateInterface $form_state) { |
63
|
|
|
/** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
64
|
|
|
$entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; |
65
|
|
|
|
66
|
|
|
$widgets = []; |
67
|
|
|
foreach ($this->widgetManager->getDefinitions() as $plugin_id => $plugin_definition) { |
68
|
|
|
$widgets[$plugin_id] = $plugin_definition['label']; |
69
|
|
|
} |
70
|
|
|
$default_widgets = []; |
71
|
|
|
foreach ($entity_browser->getWidgets() as $widget) { |
72
|
|
|
/** @var \Drupal\entity_browser\WidgetInterface $widget */ |
73
|
|
|
$default_widgets[] = $widget->id(); |
74
|
|
|
} |
75
|
|
|
$form['widget'] = [ |
76
|
|
|
'#type' => 'select', |
77
|
|
|
'#title' => $this->t('Add widget plugin'), |
78
|
|
|
'#options' => ['_none_' => '- ' . $this->t('Select a widget to add it') . ' -'] + $widgets, |
79
|
|
|
'#ajax' => [ |
80
|
|
|
'callback' => [get_class($this), 'addWidgetCallback'], |
81
|
|
|
'wrapper' => 'widgets', |
82
|
|
|
'event' => 'change' |
83
|
|
|
], |
84
|
|
|
'#executes_submit_callback' => TRUE, |
85
|
|
|
'#submit' => [[get_class($this), 'submitAddWidget']], |
86
|
|
|
'#limit_validation_errors' => [['widget']], |
87
|
|
|
]; |
88
|
|
|
$form_state->unsetValue('widget'); |
89
|
|
|
|
90
|
|
|
$form['widgets'] = [ |
91
|
|
|
'#type' => 'container', |
92
|
|
|
'#attributes' => ['id' => 'widgets'], |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$form['widgets']['table'] = [ |
96
|
|
|
'#type' => 'table', |
97
|
|
|
'#header' => [ |
98
|
|
|
$this->t('Form'), |
99
|
|
|
$this->t('Operations'), |
100
|
|
|
$this->t('Actions'), |
101
|
|
|
$this->t('Weight'), |
102
|
|
|
], |
103
|
|
|
'#empty' => $this->t('There are no widgets.'), |
104
|
|
|
'#tabledrag' => [[ |
105
|
|
|
'action' => 'order', |
106
|
|
|
'relationship' => 'sibling', |
107
|
|
|
'group' => 'variant-weight', |
108
|
|
|
]], |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
/** @var \Drupal\entity_browser\WidgetInterface $widget */ |
112
|
|
|
foreach ($entity_browser->getWidgets() as $uuid => $widget) { |
113
|
|
|
$row = [ |
114
|
|
|
'#attributes' => [ |
115
|
|
|
'class' => ['draggable'], |
116
|
|
|
], |
117
|
|
|
]; |
118
|
|
|
$row['label'] = [ |
119
|
|
|
'#type' => 'textfield', |
120
|
|
|
'#default_value' => $widget->label(), |
121
|
|
|
'#title' => $this->t('Label'), |
122
|
|
|
]; |
123
|
|
|
$row['form'] = []; |
124
|
|
|
$row['form'] = $widget->buildConfigurationForm($row['form'], $form_state); |
125
|
|
|
$row['remove'] = [ |
126
|
|
|
'#type' => 'submit', |
127
|
|
|
'#value' => $this->t('Delete'), |
128
|
|
|
'#name' => 'remove' . $uuid, |
129
|
|
|
'#ajax' => [ |
130
|
|
|
'callback' => [get_class($this), 'addWidgetCallback'], |
131
|
|
|
'wrapper' => 'widgets', |
132
|
|
|
'event' => 'click' |
133
|
|
|
], |
134
|
|
|
'#executes_submit_callback' => TRUE, |
135
|
|
|
'#submit' => [[get_class($this), 'submitDeleteWidget']], |
136
|
|
|
'#arguments' => $uuid, |
137
|
|
|
]; |
138
|
|
|
$row['weight'] = [ |
139
|
|
|
'#type' => 'weight', |
140
|
|
|
'#default_value' => $widget->getWeight(), |
141
|
|
|
'#title' => $this->t('Weight for @widget widget', ['@widget' => $widget->label()]), |
142
|
|
|
'#title_display' => 'invisible', |
143
|
|
|
'#attributes' => [ |
144
|
|
|
'class' => ['variant-weight'], |
145
|
|
|
], |
146
|
|
|
]; |
147
|
|
|
$form['widgets']['table'][$uuid] = $row; |
148
|
|
|
} |
149
|
|
|
return $form; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $form |
154
|
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
155
|
|
|
*/ |
156
|
|
|
public static function submitAddWidget($form, FormStateInterface $form_state) { |
157
|
|
|
$cached_values = $form_state->getTemporaryValue('wizard'); |
158
|
|
|
/** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
159
|
|
|
$entity_browser = $cached_values['entity_browser']; |
160
|
|
|
$widgets_num = count($entity_browser->getWidgets()); |
161
|
|
|
$widget = $form_state->getValue('widget'); |
162
|
|
|
$weight = $widgets_num + 1; |
163
|
|
|
$entity_browser->addWidget([ |
164
|
|
|
'id' => $widget, |
165
|
|
|
'label' => $widget, |
166
|
|
|
'weight' => $weight, |
167
|
|
|
// Configuration will be set on the widgets page. |
168
|
|
|
'settings' => [], |
169
|
|
|
]); |
170
|
|
|
\Drupal::service('user.shared_tempstore') |
171
|
|
|
->get('entity_browser.config') |
172
|
|
|
->set($entity_browser->id(), $cached_values); |
173
|
|
|
$form_state->setRebuild(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param $form |
178
|
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
179
|
|
|
*/ |
180
|
|
|
public static function submitDeleteWidget($form, FormStateInterface $form_state) { |
181
|
|
|
$cached_values = $form_state->getTemporaryValue('wizard'); |
182
|
|
|
/** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
183
|
|
|
$entity_browser = $cached_values['entity_browser']; |
184
|
|
|
$entity_browser->deleteWidget($entity_browser->getWidget($form_state->getTriggeringElement()['#arguments'])); |
185
|
|
|
\Drupal::service('user.shared_tempstore') |
186
|
|
|
->get('entity_browser.config') |
187
|
|
|
->set($entity_browser->id(), $cached_values); |
188
|
|
|
$form_state->setRebuild(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param $form |
193
|
|
|
* @param $form_state |
194
|
|
|
* @return mixed |
195
|
|
|
*/ |
196
|
|
|
public static function addWidgetCallback($form, $form_state) { |
197
|
|
|
return $form['widgets']; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
|
|
public function validateForm(array &$form, FormStateInterface $form_state) { |
204
|
|
|
/** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
205
|
|
|
$entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; |
206
|
|
|
/** @var \Drupal\entity_browser\WidgetInterface $widget */ |
207
|
|
|
foreach ($entity_browser->getWidgets() as $widget) { |
208
|
|
|
$widget->validateConfigurationForm($form, $form_state); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritdoc} |
214
|
|
|
*/ |
215
|
|
|
public function submitForm(array &$form, FormStateInterface $form_state) { |
216
|
|
|
/** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
217
|
|
|
$entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; |
218
|
|
|
$table = $form_state->getValue('table'); |
219
|
|
|
/** @var \Drupal\entity_browser\WidgetInterface $widget */ |
220
|
|
|
foreach ($entity_browser->getWidgets() as $uuid => $widget) { |
221
|
|
|
$widget->submitConfigurationForm($form, $form_state); |
222
|
|
|
$widget->setConfiguration([ |
223
|
|
|
'settings' => !empty($table[$uuid]['form']) ? $table[$uuid]['form'] : [], |
224
|
|
|
'weight' => $table[$uuid]['weight'], |
225
|
|
|
'label' => $table[$uuid]['label'], |
226
|
|
|
'uuid' => $uuid, |
227
|
|
|
'id' => $widget->id(), |
228
|
|
|
]); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.