-
Notifications
You must be signed in to change notification settings - Fork 9
/
webform.api.php
332 lines (299 loc) · 10.5 KB
/
webform.api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* @file
* Hooks related to Webform module.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Alter the information provided in \Drupal\webform\Annotation\WebformElement.
*
* @param array $definitions
* The array of webform handlers, keyed on the machine-readable element name.
*/
function hook_webform_element_info_alter(array &$definitions) {
}
/**
* Alter the information provided in \Drupal\webform\Annotation\WebformHandler.
*
* @param array $handlers
* The array of webform handlers, keyed on the machine-readable handler name.
*/
function hook_webform_handler_info_alter(array &$handlers) {
}
/**
* Alter definition of WebformSourceEntity plugins.
*
* @param array $definitions
* The array of plugin definitions.
*/
function hook_webform_source_entity_info_alter(array &$definitions) {
if (isset($definitions['some_plugin_whose_weight_i_wanna_change'])) {
$definitions['some_plugin_whose_weight_i_wanna_change']['weight'] = -1000;
}
}
/**
* Alter webform elements.
*
* @param array $element
* The webform element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $context
* An associative array containing the following key-value pairs:
* - form: The form structure to which elements is being attached.
*
* @see \Drupal\webform\WebformSubmissionForm::prepareElements()
* @see hook_webform_element_ELEMENT_TYPE_alter()
*/
function hook_webform_element_alter(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
// Code here acts on all elements included in a webform.
/** @var \Drupal\webform\WebformSubmissionForm $form_object */
$form_object = $form_state->getFormObject();
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $form_object->getEntity();
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $webform_submission->getWebform();
// Add custom data attributes to all elements.
$element['#attributes']['data-custom'] = '{custom data goes here}';
}
/**
* Alter webform elements for a specific type.
*
* Modules can implement hook_webform_element_ELEMENT_TYPE_alter() to
* modify a specific webform element, rather than using
* hook_webform_element_alter() and checking the element type.
*
* @param array $element
* The webform element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $context
* An associative array. See hook_field_widget_alter() for the structure
* and content of the array.
*
* @see \Drupal\webform\WebformSubmissionForm::prepareElements()
* @see hook_webform_element_alter(()
*/
function hook_webform_element_ELEMENT_TYPE_alter(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
// Add custom data attributes to a specific element type.
$element['#attributes']['data-custom'] = '{custom data goes here}';
// Attach a custom library to the element type.
$element['#attached']['library'][] = 'MODULE/MODULE.element.ELEMENT_TYPE';
}
/**
* Alter webform options.
*
* @param array $options
* An associative array of options.
* @param array $element
* The webform element that the options is for.
* @param string $options_id
* The webform options id. Set to NULL if the options are custom.
*/
function hook_webform_options_alter(array &$options, array &$element, $options_id = NULL) {
}
/**
* Alter webform options by id.
*
* @param array $options
* An associative array of options.
* @param array $element
* The webform element that the options is for.
*/
function hook_webform_options_WEBFORM_OPTIONS_ID_alter(array &$options, array &$element) {
}
/**
* Perform alterations before a webform submission form is rendered.
*
* This hook is identical to hook_form_alter() but allows the
* hook_webform_submission_form_alter() function to be stored in a dedicated
* include file and it also allows the Webform module to implement webform alter
* logic on another module's behalf.
*
* @param array $form
* Nested array of form elements that comprise the webform.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form. The arguments that
* \Drupal::formBuilder()->getForm() was originally called with are available
* in the array $form_state->getBuildInfo()['args'].
* @param string $form_id
* String representing the webform's id.
*
* @see webform.honeypot.inc
* @see hook_form_BASE_FORM_ID_alter()
* @see hook_form_FORM_ID_alter()
*
* @ingroup form_api
*/
function hook_webform_submission_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
}
/**
* Perform alterations on webform admin third party settings form.
*
* This hook is identical to hook_form_alter() but allows contrib and custom
* modules to define third party settings.
*
* @param array $form
* Nested array of form elements that comprise the webform.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @see \Drupal\webform\Form\WebformAdminSettingsForm
* @see webform.honeypot.inc
*
* @ingroup form_api
*/
function hook_webform_admin_third_party_settings_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
}
/**
* Perform alterations on webform third party settings form.
*
* This hook is identical to hook_form_alter() but allows contrib and custom
* modules to define third party settings.
*
* @param array $form
* Nested array of form elements that comprise the webform.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @see \Drupal\webform\WebformEntitySettingsForm
* @see webform.honeypot.inc
*
* @ingroup form_api
*/
function hook_webform_third_party_settings_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
}
/**
* Return information about external webform libraries.
*
* @internal
* This hook will most likely be removed once there is a
* stable release of Libraries API for Drupal 8.
*
* @see https://www.drupal.org/project/libraries
* @see \Drupal\webform\WebformLibrariesManager::initLibraries
*/
function hook_webform_libraries_info() {
}
/**
* Alter the webform module's libraries information.
*
* @internal
* This hook will most likely be removed once there is a
* stable release of Libraries API for Drupal 8.
*
* @see https://www.drupal.org/project/libraries
* @see \Drupal\webform\WebformLibrariesManager::initLibraries
*/
function hook_webform_libraries_info_alter(&$libraries) {
}
/**
* Collect extra webform help from modules.
*
* To help on-boarding new users, there is a robust help system developed. If
* you would like to add extra webform help, you are free to implement this
* hook.
*
* @return array
* Extra webform help your module is providing to the users. The return array
* should be keyed by help ID (a unique machine-name) and each sub array
* should have the following structure:
* - access: (bool) Whether the current user is granted access to this help.
* Defaults to TRUE.
* - routes: (array) Array of route names where your help should be displayed.
* - paths: (array) Array of paths where your help should be displayed. You
* can use any syntax supported by the "path.matcher" service.
* - video_id: (string) Optional video to include in the help message. Allowed
* values are the keys of WebformHelpManager::videos array.
* - attached: (array) Optional #attached array to attach to your help
* renderable array.
* - group: (string) Group where your help belongs. Allowed values are the
* keys of WebformHelpManager::groups array.
* - title: (string) Title of your help
* - content: (array) Renderable array of your help
* - message_id: (string) Optional message ID that will be supplied into
* 'webform_message' element. You are free to use 'message_*' keys if you
* want to additionally display a message when your help is displayed. These
* keyes will be supplied into 'webform_message' element. Refer to the docs
* of this element for their meaning.
* - message_type: (string) Will be supplied into 'webform_message' element.
* - message_close: (bool) Will be supplied into 'webform_message' element.
* - message_storage: (string) Will be supplied into 'webform_message'
* element.
*/
function hook_webform_help_info() {
$help = [];
$help['my_custom_help'] = [
'access' => \Drupal::currentUser()->hasPermission('my cool permission'),
'routes' => [
'my_module.route_where_i_show_this_help',
],
'paths' => [
'/path/where/*/i-wanna/show-help',
],
'video_id' => 'blocks',
'attached' => [],
'group' => 'messages',
'title' => t('Message: Webform UI Disabled'),
'content' => t('Please enable the <strong>Webform UI</strong> module if you would like to add easily add and manage elements using a drag-n-drop user interface.'),
'message_id' => '',
'message_type' => 'warning',
'message_close' => TRUE,
'message_storage' => \Drupal\webform\Element\WebformMessage::STORAGE_STATE,
];
return $help;
}
/**
* Alter the webform help.
*
* @param array $help
* Webform help data as collected from hook_webform_help_info().
*/
function hook_webform_help_info_alter(array &$help) {
if (isset($help['some_help_i_wanna_change'])) {
$help['title'] = t('This is a really cool help message. Do read it thorough!');
}
}
/**
* Act on a custom message being displayed, closed or reset.
*
* @param string $operation
* closed: Returns TRUE if the message is closed.
* close: Sets the message's state to closed.
* reset: Resets the message's closed state.
*
* @param string $id
* The message id.
*
* @return mixed|bool
* TRUE if message is closed, else NULL
*
* @internal
* This is an experimental hook whose definition may change.
*
* @see \Drupal\webform\Element\WebformMessage::isClosed
* @see \Drupal\webform\Element\WebformMessage::setClosed
* @see \Drupal\webform\Element\WebformMessage::resetClosed
*/
function hook_webform_message_custom($operation, $id) {
// Handle 'webform_test_message_custom' defined in
// webform.webform.test_element_message.yml.
if ($id === 'webform_test_message_custom') {
switch ($operation) {
case 'closed':
return \Drupal::state()->get($id, FALSE);
case 'close':
\Drupal::state()->set($id, TRUE);
return NULL;
case 'reset':
\Drupal::state()->delete($id);
return NULL;
}
}
}
/**
* @} End of "addtogroup hooks".
*/