Skip to content

Commit

Permalink
Remove webform migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Feb 20, 2024
1 parent bb6b90b commit 1e328cc
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 268 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion os2forms.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
266 changes: 0 additions & 266 deletions os2forms.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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().
*/
Expand Down

0 comments on commit 1e328cc

Please sign in to comment.