Skip to content

Commit

Permalink
[SD-482] Bug in Tide captcha blocking drush cim
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-gao committed Nov 20, 2024
1 parent 67eab3a commit 945aa2e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 19 deletions.
49 changes: 49 additions & 0 deletions modules/tide_webform/tide_webform.install
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
38 changes: 19 additions & 19 deletions modules/tide_webform/tide_webform.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -482,27 +482,27 @@ 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' => [
'all' => '-Select-',
] + $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',
],
];
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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'])) {
Expand Down Expand Up @@ -591,27 +591,27 @@ 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;
}
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);
}
Expand Down

0 comments on commit 945aa2e

Please sign in to comment.