diff --git a/CHANGELOG.md b/CHANGELOG.md index 52d4b6b..1a8de0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [3.14.1] 2024-01-16 - CprFetchData adding ajax error fix +- Remove dependency on webform migration module ## [3.14.0] 2024-01-14 diff --git a/composer.json b/composer.json index 76a8260..5134e24 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,6 @@ "drupal/webform": "^6.1", "drupal/webform_composite": "^1.0@RC", "drupal/webform_encrypt": "^2.0@alpha", - "drupal/webform_migrate": "^2.0@alpha", "drupal/webform_node_element": "^1.2", "drupal/webform_remote_handlers": "^3.0", "drupal/webform_remote_select": "^1.0.6", diff --git a/os2forms.info.yml b/os2forms.info.yml index f28e5d4..d08e8e9 100644 --- a/os2forms.info.yml +++ b/os2forms.info.yml @@ -22,7 +22,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 1357b45..f3e0b9f 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(). */