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
|
|
|
* Constructs GeneralInfoConfig form class. |
34
|
|
|
* |
35
|
|
|
* @param \Drupal\entity_browser\DisplayManager $display_manager |
|
|
|
|
36
|
|
|
* Entity browser display plugin manager. |
37
|
|
|
* @param \Drupal\entity_browser\WidgetSelectorManager $widget_selector |
|
|
|
|
38
|
|
|
* Entity browser widget selector plugin manager. |
39
|
|
|
* @param \Drupal\entity_browser\SelectionDisplayManager |
40
|
|
|
* Entity browser selection display plugin manager. |
41
|
|
|
*/ |
42
|
|
|
function __construct(WidgetManager $widget_manager, SharedTempStoreFactory $temp_store, $tempstore_id = NULL, $machine_name = NULL) { |
|
|
|
|
43
|
|
|
$this->widgetManager = $widget_manager; |
44
|
|
|
$this->tempStore = $temp_store; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public static function create(ContainerInterface $container) { |
51
|
|
|
return new static( |
52
|
|
|
$container->get('plugin.manager.entity_browser.widget'), |
53
|
|
|
$container->get('user.shared_tempstore') |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getFormId() { |
61
|
|
|
return 'entity_browser_widgets_config_form'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function buildForm(array $form, FormStateInterface $form_state) { |
68
|
|
|
/** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
69
|
|
|
$entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; |
70
|
|
|
|
71
|
|
|
$widgets = []; |
72
|
|
|
foreach ($this->widgetManager->getDefinitions() as $plugin_id => $plugin_definition) { |
73
|
|
|
$widgets[$plugin_id] = $plugin_definition['label']; |
74
|
|
|
} |
75
|
|
|
$default_widgets = []; |
76
|
|
|
foreach ($entity_browser->getWidgets() as $widget) { |
77
|
|
|
/** @var \Drupal\entity_browser\WidgetInterface $widget */ |
78
|
|
|
$default_widgets[] = $widget->id(); |
79
|
|
|
} |
80
|
|
|
$form['widget'] = [ |
81
|
|
|
'#type' => 'select', |
82
|
|
|
'#title' => $this->t('Add widget plugin'), |
83
|
|
|
'#options' => ['_none_' => '- ' . $this->t('Select a widget to add it') . ' -'] + $widgets, |
84
|
|
|
'#ajax' => [ |
85
|
|
|
'callback' => [get_class($this), 'addWidgetCallback'], |
86
|
|
|
'wrapper' => 'widgets', |
87
|
|
|
], |
88
|
|
|
'#executes_submit_callback' => TRUE, |
89
|
|
|
'#submit' => [[get_class($this), 'submitAddWidget']], |
90
|
|
|
'#limit_validation_errors' => [['widget']], |
91
|
|
|
]; |
92
|
|
|
$form_state->unsetValue('widget'); |
93
|
|
|
|
94
|
|
|
$form['widgets'] = [ |
95
|
|
|
'#type' => 'table', |
96
|
|
|
'#header' => [ |
97
|
|
|
$this->t('Form'), |
98
|
|
|
$this->t('Operations'), |
99
|
|
|
$this->t('Weight'), |
100
|
|
|
], |
101
|
|
|
'#empty' => $this->t('There are no widgets.'), |
102
|
|
|
'#tabledrag' => [[ |
103
|
|
|
'action' => 'order', |
104
|
|
|
'relationship' => 'sibling', |
105
|
|
|
'group' => 'variant-weight', |
106
|
|
|
]], |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
/** @var \Drupal\entity_browser\WidgetInterface $widget */ |
110
|
|
|
foreach ($entity_browser->getWidgets() as $widget) { |
111
|
|
|
$row = [ |
112
|
|
|
'#attributes' => [ |
113
|
|
|
'class' => ['draggable'], |
114
|
|
|
], |
115
|
|
|
]; |
116
|
|
|
$row['label'] = [ |
117
|
|
|
'#type' => 'textfield', |
118
|
|
|
'#default_value' => $widget->label(), |
119
|
|
|
'#title' => $this->t('Label'), |
120
|
|
|
]; |
121
|
|
|
$row['form'] = []; |
122
|
|
|
$row['form'] = $widget->buildConfigurationForm($row['form'], $form_state); |
123
|
|
|
$row['operations'] = []; |
124
|
|
|
$row['weight'] = [ |
125
|
|
|
'#type' => 'weight', |
126
|
|
|
'#default_value' => $widget->getWeight(), |
127
|
|
|
'#title' => $this->t('Weight for @widget widget', ['@widget' => $widget->label()]), |
128
|
|
|
'#title_display' => 'invisible', |
129
|
|
|
'#attributes' => [ |
130
|
|
|
'class' => ['variant-weight'], |
131
|
|
|
], |
132
|
|
|
]; |
133
|
|
|
$form['widgets'][$widget->uuid()] = $row; |
134
|
|
|
} |
135
|
|
|
return $form; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public static function submitAddWidget($form, FormStateInterface $form_state, $tempstore_id = NULL) { |
139
|
|
|
$cached_values = $form_state->getTemporaryValue('wizard'); |
140
|
|
|
/** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
141
|
|
|
$entity_browser = $cached_values['entity_browser']; |
142
|
|
|
$widget = $form_state->getValue('widget'); |
143
|
|
|
$entity_browser->addWidget([ |
144
|
|
|
'id' => $widget, |
145
|
|
|
'label' => $widget, |
146
|
|
|
'weight' => 0, |
147
|
|
|
// Configuration will be set on the widgets page. |
148
|
|
|
'settings' => [], |
149
|
|
|
]); |
150
|
|
|
\Drupal::service('user.shared_tempstore') |
151
|
|
|
->get('entity_browser.config') |
152
|
|
|
->set($entity_browser->id(), $cached_values); |
153
|
|
|
$form_state->setRebuild(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public static function addWidgetCallback($form, $form_state) { |
157
|
|
|
return $form['widgets']; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
View Code Duplication |
public function validateForm(array &$form, FormStateInterface $form_state) { |
|
|
|
|
164
|
|
|
/** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
165
|
|
|
$entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; |
166
|
|
|
/** @var \Drupal\entity_browser\WidgetInterface $widget */ |
167
|
|
|
foreach ($entity_browser->getWidgets() as $widget) { |
168
|
|
|
$widget->validateConfigurationForm($form, $form_state); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
public function submitForm(array &$form, FormStateInterface $form_state) { |
|
|
|
|
176
|
|
|
/** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
177
|
|
|
$entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; |
178
|
|
|
/** @var \Drupal\entity_browser\WidgetInterface $widget */ |
179
|
|
|
foreach ($entity_browser->getWidgets() as $widget) { |
180
|
|
|
$widget->submitConfigurationForm($form, $form_state); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.