From f4994a4e3a1e0673c706bd8aaaf40f0b77c21db1 Mon Sep 17 00:00:00 2001 From: Jesper Kristensen Date: Tue, 20 Feb 2024 15:15:29 +0100 Subject: [PATCH] Remove webform migrations --- CHANGELOG.md | 1 + composer.json | 1 - os2forms.info.yml | 1 - os2forms.module | 266 ---------------------------------------------- 4 files changed, 1 insertion(+), 268 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d63471c..610a4754 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] - CprFetchData adding ajax error fix +- Remove dependency on webform migration module ## [3.14.0] diff --git a/composer.json b/composer.json index 50d3d8f6..fccb9ba9 100644 --- a/composer.json +++ b/composer.json @@ -62,7 +62,6 @@ "drupal/user_default_page": "^2.1", "drupal/webform": "^6.1", "drupal/webform_composite": "^1.0@RC", - "drupal/webform_migrate": "^1.1", "drupal/webform_node_element": "^1.2", "drupal/webform_remote_handlers": "^1.6.0", "drupal/webform_remote_select": "^1.0.6", diff --git a/os2forms.info.yml b/os2forms.info.yml index 43eaf79e..f6264dca 100644 --- a/os2forms.info.yml +++ b/os2forms.info.yml @@ -23,7 +23,6 @@ dependencies: - 'drupal:webform_composite' - 'drupal:webform_entity_print' - 'drupal:webform_entity_print_attachment' - - 'drupal:webform_migrate' - 'drupal:webform_scheduled_email' - 'drupal:webform_submission_export_import' - 'drupal:webform_submission_log' diff --git a/os2forms.module b/os2forms.module index 1357b459..f3e0b9f5 100644 --- a/os2forms.module +++ b/os2forms.module @@ -6,9 +6,6 @@ */ use Drupal\Core\Form\FormStateInterface; -use Drupal\migrate\Plugin\MigrateSourceInterface; -use Drupal\migrate\Plugin\MigrationInterface; -use Drupal\migrate\Row; /** * Implements hook_entity_type_build(). @@ -65,269 +62,6 @@ function os2forms_theme() { ]; } -/** - * Implements hook_webform_migrate_d7_webform_element_ELEMENT_TYPE_alter(). - * - * Adjusts migration from Drupal 7. - * - * @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'], ['allowed_classes' => FALSE]); - $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(). - * - * Adjusts migration from Drupal 7. - * - * @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'], ['allowed_classes' => FALSE]); - 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(). - * - * Adjusts migration from Drupal 7. - * - * @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'], ['allowed_classes' => FALSE]); - 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(Row $row, MigrateSourceInterface $source, 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(Row $row, MigrateSourceInterface $source, 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'], ['allowed_classes' => FALSE]); - 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'], ['allowed_classes' => FALSE]); - 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(Row $row, MigrateSourceInterface $source, 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'], ['allowed_classes' => FALSE]); - 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); - } -} - -/** - * Preprocesses PDF template for webform. - */ -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'], ['allowed_classes' => FALSE]); - 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(). */