|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | use Drupal\Core\Form\FormStateInterface; |
9 | | -use Drupal\migrate\Plugin\MigrateSourceInterface; |
10 | | -use Drupal\migrate\Plugin\MigrationInterface; |
11 | | -use Drupal\migrate\Row; |
12 | 9 |
|
13 | 10 | /** |
14 | 11 | * Implements hook_entity_type_build(). |
@@ -65,269 +62,6 @@ function os2forms_theme() { |
65 | 62 | ]; |
66 | 63 | } |
67 | 64 |
|
68 | | -/** |
69 | | - * Implements hook_webform_migrate_d7_webform_element_ELEMENT_TYPE_alter(). |
70 | | - * |
71 | | - * Adjusts migration from Drupal 7. |
72 | | - * |
73 | | - * @See https://www.drupal.org/project/webform_migrate/issues/3155073 |
74 | | - */ |
75 | | -function os2forms_webform_migrate_d7_webform_element_date_popup_alter(&$markup, $indent, array $element) { |
76 | | - $markup .= "$indent '#type': date\n"; |
77 | | - $markup .= "$indent '#datepicker': true\n"; |
78 | | - $markup .= "$indent '#date_date_format': d/m/Y\n"; |
79 | | - |
80 | | - $extra = unserialize($element['extra'], ['allowed_classes' => FALSE]); |
81 | | - $options = [ |
82 | | - 'disabled', |
83 | | - 'readonly', |
84 | | - 'placehoder', |
85 | | - 'description', |
86 | | - ]; |
87 | | - foreach ($options as $option) { |
88 | | - if (!empty($extra[$option])) { |
89 | | - $value = 'true'; |
90 | | - switch ($option) { |
91 | | - case 'description': |
92 | | - case 'placeholder': |
93 | | - $value = "\"" . str_replace(['"', "\n", "\r"], ["'", '\n', ''], $extra[$option]) . "\""; |
94 | | - break; |
95 | | - } |
96 | | - $markup .= "$indent #$option: $value\n"; |
97 | | - } |
98 | | - } |
99 | | -} |
100 | | - |
101 | | -/** |
102 | | - * Implements hook_webform_migrate_d7_webform_element_ELEMENT_TYPE_alter(). |
103 | | - * |
104 | | - * Adjusts migration from Drupal 7. |
105 | | - * |
106 | | - * @See https://www.drupal.org/project/webform_migrate/issues/3155073 |
107 | | - */ |
108 | | -function os2forms_webform_migrate_d7_webform_element_bt_layout_row_alter(&$markup, $indent, array $element) { |
109 | | - $markup .= "$indent '#type': webform_flexbox\n"; |
110 | | - |
111 | | - $extra = unserialize($element['extra'], ['allowed_classes' => FALSE]); |
112 | | - if (!empty($extra['description'])) { |
113 | | - $value = "\"" . str_replace(['"', "\n", "\r"], ["'", '\n', ''], $extra['description']) . "\""; |
114 | | - $markup .= "$indent #description: $value\n"; |
115 | | - } |
116 | | -} |
117 | | - |
118 | | -/** |
119 | | - * Implements hook_webform_migrate_d7_webform_element_ELEMENT_TYPE_alter(). |
120 | | - * |
121 | | - * Adjusts migration from Drupal 7. |
122 | | - * |
123 | | - * @See https://www.drupal.org/project/webform_migrate/issues/3155073 |
124 | | - */ |
125 | | -function os2forms_webform_migrate_d7_webform_element_bt_layout_cols_alter(&$markup, $indent, array $element) { |
126 | | - $markup .= "$indent '#type': container\n"; |
127 | | - $extra = unserialize($element['extra'], ['allowed_classes' => FALSE]); |
128 | | - if (!empty($extra['description'])) { |
129 | | - $value = "\"" . str_replace(['"', "\n", "\r"], ["'", '\n', ''], $extra['description']) . "\""; |
130 | | - $markup .= "$indent #description: $value\n"; |
131 | | - } |
132 | | -} |
133 | | - |
134 | | -/** |
135 | | - * Implements hook_migrate_prepare_row(). |
136 | | - */ |
137 | | -function os2forms_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) { |
138 | | - _os2forms_migrate_prepare_row_pdf($row, $source, $migration); |
139 | | - _os2forms_migrate_prepare_row_doc($row, $source, $migration); |
140 | | - /** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */ |
141 | | - $migrate_to_category = \Drupal::service('webform.third_party_settings_manager')->getThirdPartySetting('os2forms', 'migrate_to_category'); |
142 | | - if (!empty($migrate_to_category)) { |
143 | | - $row->setDestinationProperty('category', $migrate_to_category); |
144 | | - } |
145 | | -} |
146 | | - |
147 | | -/** |
148 | | - * Implements support of PDF attachment migration. |
149 | | - */ |
150 | | -function _os2forms_migrate_prepare_row_pdf(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) { |
151 | | - // Adding migration path for Webform PDF Entity print attachment. |
152 | | - $nid = $row->getSourceProperty('nid'); |
153 | | - $default = $source->getDatabase()->select('webform2pdf', 'w2p') |
154 | | - ->fields('w2p') |
155 | | - ->condition('nid', $nid, '=') |
156 | | - ->execute() |
157 | | - ->fetchAssoc(); |
158 | | - |
159 | | - if (!empty($default['data'])) { |
160 | | - $data = unserialize($default['data'], ['allowed_classes' => FALSE]); |
161 | | - unset($default['data']); |
162 | | - $default = array_merge($default, $data); |
163 | | - } |
164 | | - |
165 | | - if (empty($default['enabled'])) { |
166 | | - return; |
167 | | - } |
168 | | - |
169 | | - $of_default = $source->getDatabase()->select('os2forms_frontend_webform_settings', 'of') |
170 | | - ->fields('of') |
171 | | - ->condition('nid', $nid, '=') |
172 | | - ->condition('submodule', 'os2forms_webform2pdf', '=') |
173 | | - ->execute() |
174 | | - ->fetchAssoc(); |
175 | | - |
176 | | - if (!empty($of_default['data'])) { |
177 | | - $data = unserialize($of_default['data'], ['allowed_classes' => FALSE]); |
178 | | - unset($of_default['data']); |
179 | | - $default = array_merge($of_default, $data); |
180 | | - } |
181 | | - |
182 | | - $markup = "webform_attachment_pdf:\n"; |
183 | | - $markup .= " '#title': 'PDF Attachment'\n"; |
184 | | - $markup .= " '#type': webform_entity_print_attachment:pdf\n"; |
185 | | - $filename = 'webform_submission-[webform:id]-[webform_submission:sid].pdf'; |
186 | | - if ($default['custom_pdf_name']) { |
187 | | - $filename = str_replace( |
188 | | - ['@nid', '@sid'], |
189 | | - ['[webform:id]', '[webform_submission:sid]'], |
190 | | - $data['custom_pdf_name'] |
191 | | - ); |
192 | | - } |
193 | | - $markup .= " '#filename': '$filename'\n"; |
194 | | - $template = _os2forms_migrate_prepare_webform2pdf_get_template($nid, $source); |
195 | | - if ($template) { |
196 | | - $template_arr = explode("\n", $template); |
197 | | - $markup .= " '#view_mode': twig\n"; |
198 | | - $markup .= " '#template': |\n " . implode("\n ", $template_arr) . "\n"; |
199 | | - } |
200 | | - |
201 | | - // Saving new form element. |
202 | | - $elements = $row->getSourceProperty('elements'); |
203 | | - $elements .= $markup; |
204 | | - $row->setSourceProperty('elements', $elements); |
205 | | - $dependencies = $row->getDestinationProperty('dependencies'); |
206 | | - if (empty($dependencies) || !array_search('os2forms', $dependencies)) { |
207 | | - $dependencies['module'][] = 'os2forms'; |
208 | | - $row->setDestinationProperty('dependencies', $dependencies); |
209 | | - } |
210 | | -} |
211 | | - |
212 | | -/** |
213 | | - * Implements support of DOC attachment migration. |
214 | | - */ |
215 | | -function _os2forms_migrate_prepare_row_doc(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) { |
216 | | - // Adding migration path for Webform DOC Entity print attachment. |
217 | | - $nid = $row->getSourceProperty('nid'); |
218 | | - $default = $source->getDatabase()->select('os2forms_frontend_webform_settings', 'of') |
219 | | - ->fields('of') |
220 | | - ->condition('nid', $nid, '=') |
221 | | - ->condition('submodule', 'os2forms_doc', '=') |
222 | | - ->execute() |
223 | | - ->fetchAssoc(); |
224 | | - |
225 | | - if (!empty($default['data'])) { |
226 | | - $data = unserialize($default['data'], ['allowed_classes' => FALSE]); |
227 | | - unset($default['data']); |
228 | | - $default = array_merge($default, $data); |
229 | | - } |
230 | | - |
231 | | - if (empty($default['enabled'])) { |
232 | | - return; |
233 | | - } |
234 | | - |
235 | | - $markup = "webform_attachment_docx:\n"; |
236 | | - $markup .= " '#title': 'PDF Attachment'\n"; |
237 | | - $markup .= " '#type': webform_entity_print_attachment:word_docx\n"; |
238 | | - $markup .= " '#filename': 'webform_submission-[webform:id]-[webform_submission:sid].docx'\n"; |
239 | | - $template = _os2forms_migrate_prepare_webform2pdf_get_template($nid, $source); |
240 | | - if ($template) { |
241 | | - $template_arr = explode("\n", $template); |
242 | | - $markup .= " '#view_mode': twig\n"; |
243 | | - $markup .= " '#template': |\n " . implode("\n ", $template_arr) . "\n"; |
244 | | - } |
245 | | - |
246 | | - // Saving new form element. |
247 | | - $elements = $row->getSourceProperty('elements'); |
248 | | - $elements .= $markup; |
249 | | - $row->setSourceProperty('elements', $elements); |
250 | | - |
251 | | - $dependencies = $row->getDestinationProperty('dependencies'); |
252 | | - if (empty($dependencies) || !array_search('os2forms', $dependencies)) { |
253 | | - $dependencies['module'][] = 'os2forms'; |
254 | | - $row->setDestinationProperty('dependencies', $dependencies); |
255 | | - } |
256 | | -} |
257 | | - |
258 | | -/** |
259 | | - * Preprocesses PDF template for webform. |
260 | | - */ |
261 | | -function _os2forms_migrate_prepare_webform2pdf_get_template($nid, $source) { |
262 | | - $template = &drupal_static(__FUNCTION__ . '_' . $nid, FALSE); |
263 | | - if ($template) { |
264 | | - return $template; |
265 | | - } |
266 | | - $settings = $source->getDatabase()->select('webform2pdf', 'w2p') |
267 | | - ->fields('w2p') |
268 | | - ->condition('nid', $nid, '=') |
269 | | - ->execute() |
270 | | - ->fetchAssoc(); |
271 | | - |
272 | | - if (!empty($settings['data'])) { |
273 | | - $data = unserialize($settings['data'], ['allowed_classes' => FALSE]); |
274 | | - unset($settings['data']); |
275 | | - $settings = array_merge($settings, $data); |
276 | | - } |
277 | | - |
278 | | - if (empty($settings['p_body']['value'])) { |
279 | | - return FALSE; |
280 | | - } |
281 | | - $template = $settings['p_body']['value']; |
282 | | - |
283 | | - // Due to changed form_keys we should replace old form_key to new one. |
284 | | - // Collecting mapping array for form_keys. |
285 | | - $query = $source->getDatabase()->select('webform_component', 'wc'); |
286 | | - $query->fields('wc', [ |
287 | | - 'pid', |
288 | | - 'form_key', |
289 | | - ]); |
290 | | - $components = $query->condition('nid', $nid)->execute(); |
291 | | - $keys = []; |
292 | | - foreach ($components as $component) { |
293 | | - $keys[$component->form_key] = $component->form_key . ($component->pid ? '_' . $component->pid : ''); |
294 | | - } |
295 | | - |
296 | | - // Replacing D7 webform tokens to D8. |
297 | | - $template = str_replace('[submission:', '[webform_submission:', $template); |
298 | | - foreach ($keys as $old_key => $new_key) { |
299 | | - $template = str_replace(':' . $old_key . ']', ':' . $new_key . ']', $template); |
300 | | - $template = str_replace(':' . $old_key . ':', ':' . $new_key . ':', $template); |
301 | | - } |
302 | | - |
303 | | - // Check valid tokens. |
304 | | - preg_match_all('/\[(.*)\]/', $template, $matches); |
305 | | - $tokens = array_unique($matches[0]); |
306 | | - $valid_tokens = array_filter($tokens, function ($token) { |
307 | | - return empty(\Drupal::token()->getInvalidTokensByContext($token, [ |
308 | | - 0 => 'webform', |
309 | | - 1 => 'webform_submission', |
310 | | - 2 => 'webform_handler', |
311 | | - 3 => 'site', |
312 | | - 4 => 'date', |
313 | | - ])); |
314 | | - }); |
315 | | - |
316 | | - // Making tokens available for twig template. |
317 | | - foreach ($valid_tokens as $token) { |
318 | | - $template = str_replace($token, "{{ webform_token('" . $token . "', webform_submission, [], options) }}", $template); |
319 | | - $unset_key = array_search($token, $tokens); |
320 | | - unset($tokens[$unset_key]); |
321 | | - } |
322 | | - |
323 | | - // Removing invalid tokens. |
324 | | - foreach ($tokens as $invalid_token) { |
325 | | - $template = str_replace($invalid_token, '', $template); |
326 | | - } |
327 | | - |
328 | | - return $template; |
329 | | -} |
330 | | - |
331 | 65 | /** |
332 | 66 | * Implements hook_menu_local_tasks_alter(). |
333 | 67 | */ |
|
0 commit comments