-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathos2forms.module
336 lines (301 loc) · 11.4 KB
/
os2forms.module
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
333
334
335
336
<?php
/**
* @file
* OS2Forms functionality module.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_webform_third_party_settings_form_alter().
*/
function os2forms_webform_third_party_settings_form_alter(&$form, FormStateInterface $form_state) {
// OS2Forms.
$form['third_party_settings']['os2forms'] = [
'#type' => 'details',
'#title' => t('OS2Forms'),
'#open' => TRUE,
'#description' => t('OS2Forms related settings'),
];
}
/**
* Implements hook_webform_admin_third_party_settings_form_alter().
*/
function os2forms_webform_admin_third_party_settings_form_alter(&$form, FormStateInterface $form_state) {
$form['third_party_settings']['os2forms'] = [
'#type' => 'details',
'#title' => t('OS2Forms'),
];
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$webform_entity_storage = \Drupal::service('entity_type.manager')->getStorage('webform');
$form['third_party_settings']['os2forms']['migrate_to_category'] = [
'#type' => 'webform_select_other',
'#title' => t('Migrate webforms to category'),
'#description' => t('After migration webforms will get this category.'),
'#options' => $webform_entity_storage->getCategories(),
'#empty_value' => '',
'#default_value' => $third_party_settings_manager->getThirdPartySetting('os2forms', 'migrate_to_category') ?: FALSE,
];
}
/**
* Implements hook_theme().
*/
function os2forms_theme() {
return [
'webform_handler_save_to_file_summary' => [
'variables' => ['settings' => NULL, 'handler' => NULL],
],
];
}
/**
* Implements hook_webform_migrate_d7_webform_element_ELEMENT_TYPE_alter().
*
* Not released hook
* @See https://www.drupal.org/project/webform_migrate/issues/3155073
*/
function os2forms_webform_migrate_d7_webform_element_date_popup_alter(&$markup, $indent, array $element) {
$markup .= "$indent '#type': date\n";
$markup .= "$indent '#datepicker': true\n";
$markup .= "$indent '#date_date_format': d/m/Y\n";
$extra = unserialize($element['extra']);
$options = [
'disabled',
'readonly',
'placehoder',
'description',
];
foreach ($options as $option) {
if (!empty($extra[$option])) {
$value = 'true';
switch ($option) {
case 'description':
case 'placeholder':
$value = "\"" . str_replace(['"', "\n", "\r"], ["'", '\n', ''], $extra[$option]) . "\"";
break;
}
$markup .= "$indent #$option: $value\n";
}
}
}
/**
* Implements hook_webform_migrate_d7_webform_element_ELEMENT_TYPE_alter().
*
* Not released hook
* @See https://www.drupal.org/project/webform_migrate/issues/3155073
*/
function os2forms_webform_migrate_d7_webform_element_bt_layout_row_alter(&$markup, $indent, array $element) {
$markup .= "$indent '#type': webform_flexbox\n";
$extra = unserialize($element['extra']);
if (!empty($extra['description'])) {
$value = "\"" . str_replace(['"', "\n", "\r"], ["'", '\n', ''], $extra['description']) . "\"";
$markup .= "$indent #description: $value\n";
}
}
/**
* Implements hook_webform_migrate_d7_webform_element_ELEMENT_TYPE_alter().
*
* Not released hook
* @See https://www.drupal.org/project/webform_migrate/issues/3155073
*/
function os2forms_webform_migrate_d7_webform_element_bt_layout_cols_alter(&$markup, $indent, array $element) {
$markup .= "$indent '#type': container\n";
$extra = unserialize($element['extra']);
if (!empty($extra['description'])) {
$value = "\"" . str_replace(['"', "\n", "\r"], ["'", '\n', ''], $extra['description']) . "\"";
$markup .= "$indent #description: $value\n";
}
}
/**
* Implements hook_migrate_prepare_row().
*/
function os2forms_migrate_prepare_row(\Drupal\migrate\Row $row, \Drupal\migrate\Plugin\MigrateSourceInterface $source, \Drupal\migrate\Plugin\MigrationInterface $migration) {
_os2forms_migrate_prepare_row_pdf($row, $source, $migration);
_os2forms_migrate_prepare_row_doc($row, $source, $migration);
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$migrate_to_category = \Drupal::service('webform.third_party_settings_manager')->getThirdPartySetting('os2forms', 'migrate_to_category');
if (!empty($migrate_to_category)) {
$row->setDestinationProperty('category', $migrate_to_category);
}
}
/**
* Implements support of PDF attachment migration.
*/
function _os2forms_migrate_prepare_row_pdf(\Drupal\migrate\Row $row, \Drupal\migrate\Plugin\MigrateSourceInterface $source, \Drupal\migrate\Plugin\MigrationInterface $migration) {
// Adding migration path for Webform PDF Entity print attachment.
$nid = $row->getSourceProperty('nid');
$default = $source->getDatabase()->select('webform2pdf', 'w2p')
->fields('w2p')
->condition('nid', $nid, '=')
->execute()
->fetchAssoc();
if (!empty($default['data'])) {
$data = unserialize($default['data']);
unset($default['data']);
$default = array_merge($default, $data);
}
if (empty($default['enabled'])) {
return;
}
$of_default = $source->getDatabase()->select('os2forms_frontend_webform_settings', 'of')
->fields('of')
->condition('nid', $nid, '=')
->condition('submodule', 'os2forms_webform2pdf', '=')
->execute()
->fetchAssoc();
if (!empty($of_default['data'])) {
$data = unserialize($of_default['data']);
unset($of_default['data']);
$default = array_merge($of_default, $data);
}
$markup = "webform_attachment_pdf:\n";
$markup .= " '#title': 'PDF Attachment'\n";
$markup .= " '#type': webform_entity_print_attachment:pdf\n";
$filename = 'webform_submission-[webform:id]-[webform_submission:sid].pdf';
if ($default['custom_pdf_name']) {
$filename = str_replace(['@nid', '@sid'], ['[webform:id]', '[webform_submission:sid]'], $data['custom_pdf_name']);
}
$markup .= " '#filename': '$filename'\n";
$template = _os2forms_migrate_prepare_webform2pdf_get_template($nid, $source);
if ($template) {
$template_arr = explode("\n", $template);
$markup .= " '#view_mode': twig\n";
$markup .= " '#template': |\n " . implode("\n ", $template_arr) . "\n";
}
// Saving new form element.
$elements = $row->getSourceProperty('elements');
$elements .= $markup;
$row->setSourceProperty('elements', $elements);
$dependencies = $row->getDestinationProperty('dependencies');
if (empty($dependencies) || !array_search('os2forms', $dependencies)) {
$dependencies['module'][] = 'os2forms';
$row->setDestinationProperty('dependencies', $dependencies);
}
}
/**
* Implements support of DOC attachment migration.
*/
function _os2forms_migrate_prepare_row_doc(\Drupal\migrate\Row $row, \Drupal\migrate\Plugin\MigrateSourceInterface $source, \Drupal\migrate\Plugin\MigrationInterface $migration) {
// Adding migration path for Webform DOC Entity print attachment.
$nid = $row->getSourceProperty('nid');
$default = $source->getDatabase()->select('os2forms_frontend_webform_settings', 'of')
->fields('of')
->condition('nid', $nid, '=')
->condition('submodule', 'os2forms_doc', '=')
->execute()
->fetchAssoc();
if (!empty($default['data'])) {
$data = unserialize($default['data']);
unset($default['data']);
$default = array_merge($default, $data);
}
if (empty($default['enabled'])) {
return;
}
$markup = "webform_attachment_docx:\n";
$markup .= " '#title': 'PDF Attachment'\n";
$markup .= " '#type': webform_entity_print_attachment:word_docx\n";
$markup .= " '#filename': 'webform_submission-[webform:id]-[webform_submission:sid].docx'\n";
$template = _os2forms_migrate_prepare_webform2pdf_get_template($nid, $source);
if ($template) {
$template_arr = explode("\n", $template);
$markup .= " '#view_mode': twig\n";
$markup .= " '#template': |\n " . implode("\n ", $template_arr) . "\n";
}
// Saving new form element.
$elements = $row->getSourceProperty('elements');
$elements .= $markup;
$row->setSourceProperty('elements', $elements);
$dependencies = $row->getDestinationProperty('dependencies');
if (empty($dependencies) || !array_search('os2forms', $dependencies)) {
$dependencies['module'][] = 'os2forms';
$row->setDestinationProperty('dependencies', $dependencies);
}
}
function _os2forms_migrate_prepare_webform2pdf_get_template($nid, $source) {
$template = &drupal_static(__FUNCTION__ . '_' . $nid, FALSE);
if ($template) {
return $template;
}
$settings = $source->getDatabase()->select('webform2pdf', 'w2p')
->fields('w2p')
->condition('nid', $nid, '=')
->execute()
->fetchAssoc();
if (!empty($settings['data'])) {
$data = unserialize($settings['data']);
unset($settings['data']);
$settings = array_merge($settings, $data);
}
if (empty($settings['p_body']['value'])) {
return FALSE;
}
$template = $settings['p_body']['value'];
// Due to changed form_keys we should replace old form_key to new one.
// Collecting mapping array for form_keys.
$query = $source->getDatabase()->select('webform_component', 'wc');
$query->fields('wc', [
'pid',
'form_key',
]);
$components = $query->condition('nid', $nid)->execute();
$keys = [];
foreach ($components as $component) {
$keys[$component->form_key] = $component->form_key . ($component->pid ? '_' . $component->pid : '');
}
// Replacing D7 webform tokens to D8.
$template = str_replace('[submission:', '[webform_submission:', $template);
foreach ($keys as $old_key => $new_key) {
$template = str_replace(':' . $old_key . ']', ':' . $new_key . ']', $template);
$template = str_replace(':' . $old_key . ':', ':' . $new_key . ':', $template);
}
// Check valid tokens.
preg_match_all('/\[(.*)\]/', $template, $matches);
$tokens = array_unique($matches[0]);
$valid_tokens = array_filter($tokens, function($token) {
return empty(\Drupal::token()->getInvalidTokensByContext($token, [
0 => 'webform',
1 => 'webform_submission',
2 => 'webform_handler',
3 => 'site',
4 => 'date',
]));
});
// Making tokens available for twig template.
foreach($valid_tokens as $token) {
$template = str_replace($token, "{{ webform_token('" . $token . "', webform_submission, [], options) }}", $template);
$unset_key = array_search($token, $tokens);
unset($tokens[$unset_key]);
}
// Removing invalid tokens.
foreach($tokens as $invalid_token) {
$template = str_replace($invalid_token, '', $template);
}
return $template;
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function os2forms_menu_local_tasks_alter(&$data, $route_name) {
$webform = \Drupal::routeMatch()->getParameter('webform');
$routeName = \Drupal::routeMatch()->getRouteName();
if ($webform && $routeName == $route_name) {
if (is_object($webform)) {
$webform = $webform->id();
}
if (isset ($data['tabs'][0]['entity.webform.embed']) && !\Drupal::currentUser()->hasPermission('os2forms embed webform access') ) {
unset($data['tabs'][0]['entity.webform.embed']);
}
}
}
/**
* Implements hook_module_implements_alter().
*/
function os2forms_module_implements_alter(&$implementations, $hook) {
switch ($hook) {
// Move our hook_entity_type_alter() implementation to the end of the list.
case 'menu_local_tasks_alter':
$group = $implementations['os2forms'];
unset($implementations['os2forms']);
$implementations['os2forms'] = $group;
break;
}
}