diff --git a/modules/tide_webform/tide_webform.install b/modules/tide_webform/tide_webform.install index 0370804dd..dff545b96 100644 --- a/modules/tide_webform/tide_webform.install +++ b/modules/tide_webform/tide_webform.install @@ -52,3 +52,52 @@ function tide_webform_update_10002() { } } } + +/** + * Update tide webform dependencies. + */ +function update_tide_webform_dependencies(array $dependencies, array $remove_modules = [], array $add_modules = []) { + if (isset($dependencies['module'])) { + $dependencies['module'] = array_values( + array_diff($dependencies['module'], $remove_modules) + ); + $dependencies['module'] = array_unique( + array_merge($dependencies['module'], $add_modules) + ); + sort($dependencies['module']); + } + return $dependencies; +} + +/** + * Update tide webform captcha settings. + */ +function update_tide_webform_captcha_settings(array $config) { + if (isset($config['tide_webform_captcha'])) { + $settings = $config['tide_webform_captcha']; + unset($config['tide_webform_captcha']); + $config['tide_webform'] = $settings; + } + return $config; +} + +/** + * Update webform dependencies and third party settings. + */ +function tide_webform_update_10003() { + $webform_storage = \Drupal::entityTypeManager()->getStorage('webform'); + $webforms = $webform_storage->loadMultiple(); + foreach ($webforms as $webform_id => $webform_entity) { + $config = \Drupal::configFactory()->getEditable('webform.webform.' . $webform_id); + $dependencies = $config->get('dependencies'); + $third_party_settings = $config->get('third_party_settings'); + if (!empty($dependencies)) { + $result = update_tide_webform_dependencies($dependencies, ['tide_webform_captcha'], ['tide_webform']); + $config->set('dependencies', $result)->save(); + } + if (!empty($third_party_settings)) { + $result = update_tide_webform_captcha_settings($third_party_settings); + $config->set('third_party_settings', $result)->save(); + } + } +} diff --git a/modules/tide_webform/tide_webform.module b/modules/tide_webform/tide_webform.module index 976647998..2ac1f487c 100644 --- a/modules/tide_webform/tide_webform.module +++ b/modules/tide_webform/tide_webform.module @@ -445,7 +445,7 @@ function tide_webform_webform_update(EntityInterface $entity) { */ function tide_webform_webform_third_party_settings_form_alter(array &$form, FormStateInterface $form_state) { $webform = $form_state->getFormObject()->getEntity(); - $third_party_settings = $webform->getThirdPartySettings('tide_webform_captcha'); + $third_party_settings = $webform->getThirdPartySettings('tide_webform'); $user_input = $form_state->getUserInput(); $taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); $captcha_type = NULL; @@ -469,8 +469,8 @@ function tide_webform_webform_third_party_settings_form_alter(array &$form, Form if (isset($third_party_settings['captcha_type'])) { $captcha_type = $third_party_settings['captcha_type']; } - if (isset($user_input['third_party_settings']['tide_webform_captcha']['captcha_type'])) { - $captcha_type = $user_input['third_party_settings']['tide_webform_captcha']['captcha_type']; + if (isset($user_input['third_party_settings']['tide_webform']['captcha_type'])) { + $captcha_type = $user_input['third_party_settings']['tide_webform']['captcha_type']; } $query = $taxonomy_storage->getQuery() ->accessCheck(TRUE) @@ -482,19 +482,19 @@ function tide_webform_webform_third_party_settings_form_alter(array &$form, Form return $term->label() . ' (' . $term->id() . ')'; }, $terms); - $form['third_party_settings']['tide_webform_captcha'] = [ + $form['third_party_settings']['tide_webform'] = [ '#type' => 'fieldset', '#title' => t('Tide webform CAPTCHA'), '#open' => TRUE, ]; - $form['third_party_settings']['tide_webform_captcha']['enable_captcha'] = [ + $form['third_party_settings']['tide_webform']['enable_captcha'] = [ '#type' => 'checkbox', '#default_value' => !empty($third_party_settings['enable_captcha']) ? $third_party_settings['enable_captcha'] : NULL, '#title' => t('Enable captcha'), ]; - $form['third_party_settings']['tide_webform_captcha']['captcha_type'] = [ + $form['third_party_settings']['tide_webform']['captcha_type'] = [ '#type' => 'select', '#title' => t('Captcha type'), '#options' => [ @@ -502,7 +502,7 @@ function tide_webform_webform_third_party_settings_form_alter(array &$form, Form ] + $allowed_values, '#default_value' => !empty($third_party_settings['captcha_type']) ? $third_party_settings['captcha_type'] : NULL, '#ajax' => [ - 'callback' => '_tide_webform_captcha_type_dropdown_callback', + 'callback' => '_tide_webform_type_dropdown_callback', 'wrapper' => 'captcha-type-dropdown-container', ], ]; @@ -514,7 +514,7 @@ function tide_webform_webform_third_party_settings_form_alter(array &$form, Form $score_threshold = (string) $third_party_settings['score_threshold']; } - $form['third_party_settings']['tide_webform_captcha']['score_threshold'] = [ + $form['third_party_settings']['tide_webform']['score_threshold'] = [ '#type' => 'textfield', '#title' => t('Score threshold (reCAPTCHA v3)'), '#size' => 2, @@ -523,19 +523,19 @@ function tide_webform_webform_third_party_settings_form_alter(array &$form, Form '#element_validate' => ['_tide_webform_threshold_validate'], '#states' => [ 'visible' => [ - ':input[name="third_party_settings[tide_webform_captcha][captcha_type]"]' => ['value' => 'google_recaptcha_v3'], + ':input[name="third_party_settings[tide_webform][captcha_type]"]' => ['value' => 'google_recaptcha_v3'], ], ], '#default_value' => $score_threshold, '#description' => 'Enter a value between 0.0 and 1.0. Use only one decimal place (e.g., 0.0, 0.5, 1.0).', ]; - $form['third_party_settings']['tide_webform_captcha']['captcha_type_dropdown_container'] = [ + $form['third_party_settings']['tide_webform']['captcha_type_dropdown_container'] = [ '#type' => 'fieldset', '#attributes' => ['id' => 'captcha-type-dropdown-container'], ]; - $form['third_party_settings']['tide_webform_captcha']['captcha_type_dropdown_container']['captcha_details'] = [ + $form['third_party_settings']['tide_webform']['captcha_type_dropdown_container']['captcha_details'] = [ '#type' => \Drupal::moduleHandler()->moduleExists('select2') ? 'select2' : 'select', '#title' => ('Site key'), '#target_type' => 'taxonomy_term', @@ -553,14 +553,14 @@ function tide_webform_webform_third_party_settings_form_alter(array &$form, Form ], ]; - $form['#validate'][] = '_tide_webform_captcha_form_validate'; + $form['#validate'][] = '_tide_webform_form_validate'; } /** * Captcha form validate. */ -function _tide_webform_captcha_form_validate(&$form, FormStateInterface $form_state) { - $settings = &$form_state->getValue(['third_party_settings', 'tide_webform_captcha']); +function _tide_webform_form_validate(&$form, FormStateInterface $form_state) { + $settings = &$form_state->getValue(['third_party_settings', 'tide_webform']); $captcha_details = $settings['captcha_type_dropdown_container']['captcha_details'] ?? NULL; if (is_array($captcha_details) && isset($captcha_details[0]['target_id'])) { @@ -591,19 +591,19 @@ function _tide_webform_captcha_form_validate(&$form, FormStateInterface $form_st /** * Captcha options callback. */ -function _tide_webform_captcha_type_dropdown_callback($form, FormStateInterface $form_state) { - return $form['third_party_settings']['tide_webform_captcha']['captcha_type_dropdown_container']; +function _tide_webform_type_dropdown_callback($form, FormStateInterface $form_state) { + return $form['third_party_settings']['tide_webform']['captcha_type_dropdown_container']; } /** * Threshold element validation. */ function _tide_webform_threshold_validate($element, FormStateInterface $form_state) { - $number = $form_state->getUserInput()['third_party_settings']['tide_webform_captcha']['score_threshold']; + $number = $form_state->getUserInput()['third_party_settings']['tide_webform']['score_threshold']; if ($number === 0 || $number === '' || $number === NULL) { $form_state->setValue([ 'third_party_settings', - 'tide_webform_captcha', + 'tide_webform', 'score_threshold', ], NULL); return; @@ -611,7 +611,7 @@ function _tide_webform_threshold_validate($element, FormStateInterface $form_sta if (preg_match('/^(0(\.[0-9])?|1(\.0)?)$/', (string) $number)) { $form_state->setValue([ 'third_party_settings', - 'tide_webform_captcha', + 'tide_webform', 'score_threshold', ], (float) $number); }