Skip to content

Commit

Permalink
VACMS-17896: Allows all forms or selected forms.
Browse files Browse the repository at this point in the history
  • Loading branch information
omahane committed Jan 13, 2025
1 parent fd1bd17 commit e5aea27
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 44 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@
"3254663 - Notice: Undefined index: target_bundles on Drupal\\cer\\Entity\\CorrespondingReference->synchronizeCorrespondingField()": "patches/3254669-prevent-undefined-index.patch"
},
"drupal/clientside_validation": {
"2949540 - Allow specific form ids for clientside validation": "patches/2949540-allow-specific-form-ids-for-clientside-validation.patch"
"2949540 - Allow all forms or selected forms": "patches/2949540-allow-all-forms-selected-forms.patch"
},
"drupal/content_lock": {
"2951652 - Content lock causes redirect response preventing migration rollback.": "patches/2951652-content-lock-causes-redirect-response-preventing-migration-rollback.patch"
Expand Down
2 changes: 1 addition & 1 deletion composer.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "f9cd350a249e3227e672591dc48416f7",
"content-hash": "a5f401772dc851352852afd469b1ee8d",
"packages": [
{
"name": "asm89/stack-cors",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
diff --git a/clientside_validation.install b/clientside_validation.install
new file mode 100644
index 0000000000000000000000000000000000000000..a7a419250ea95ed5e19ff966fb9e53916f2c0f3c
--- /dev/null
+++ b/clientside_validation.install
@@ -0,0 +1,24 @@
+<?php
+/**
+ * Update configuration for validation_scope based on enable_all_forms.
+ */
+function clientside_validation_update_10001() {
+ // Load the existing configuration.
+ $config = \Drupal::configFactory()->getEditable('clientside_validation.settings');
+
+ // Check the value of enable_all_forms and set validation_scope accordingly.
+ if ($config->get('enable_all_forms')) {
+ // If enable_all_forms is checked, set validation_scope the first choice.
+ $config->set('validation_scope', '0');
+ } else {
+ $config->set('validation_scope', '1');
+ }
+
+ // Set the enabled_forms to selected_forms.
+ if ($config->get('enabled_forms')) {
+ $config->set('selected_forms', $config->get('enabled_forms'));
+ }
+
+ // Save the updated configuration.
+ $config->save();
+}
diff --git a/clientside_validation.links.menu.yml b/clientside_validation.links.menu.yml
new file mode 100644
index 0000000..0803f80
index 0000000000000000000000000000000000000000..0803f80165dab49b5d93a0ace129aba621d4e9e3
--- /dev/null
+++ b/clientside_validation.links.menu.yml
@@ -0,0 +1,5 @@
Expand All @@ -10,10 +40,10 @@ index 0000000..0803f80
+ route_name: clientside_validation.settings_form
+ parent: system.admin_config_ui
diff --git a/clientside_validation.module b/clientside_validation.module
index 50fa8b3..6da4e71 100644
index 50fa8b3e85a750e70e3bed66b2b8432df89905b6..74d1cac29adc581ba0d05b697593970848f1bc4b 100644
--- a/clientside_validation.module
+++ b/clientside_validation.module
@@ -12,7 +12,49 @@ use Drupal\Core\Render\Element;
@@ -12,7 +12,55 @@ use Drupal\Core\Render\Element;
* Implements hook_form_alter().
*/
function clientside_validation_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
Expand All @@ -29,14 +59,20 @@ index 50fa8b3..6da4e71 100644
+ }
+
+ // If enabled for all forms, add the after build function.
+ $enable_all_forms = $config->get('enable_all_forms');
+ if ($enable_all_forms) {
+ $validation_scope = $config->get('validation_scope');
+ if ($validation_scope === '0') {
+ $form['#after_build'][] = 'clientside_validation_form_after_build';
+ }
+ // Else, add it only if the form ID was added in configuration.
+ else {
+ $enabled_forms = $config->get('enabled_forms');
+ if (!empty($enabled_forms) && in_array($form_id, $enabled_forms)) {
+ elseif ($validation_scope === '1') {
+ $selected_forms = $config->get('selected_forms');
+ if (!empty($selected_forms) && in_array($form_id, $selected_forms)) {
+ $form['#after_build'][] = 'clientside_validation_form_after_build';
+ }
+ }
+ elseif ($validation_scope === '2') {
+ $selected_forms = $config->get('selected_forms');
+ if (!empty($selected_forms) && !in_array($form_id, $selected_forms)) {
+ $form['#after_build'][] = 'clientside_validation_form_after_build';
+ }
+ }
Expand Down Expand Up @@ -66,7 +102,7 @@ index 50fa8b3..6da4e71 100644
/**
diff --git a/clientside_validation.permissions.yml b/clientside_validation.permissions.yml
new file mode 100644
index 0000000..579ecaa
index 0000000000000000000000000000000000000000..579ecaab512f3f2d28655a2122d78dc4643591ac
--- /dev/null
+++ b/clientside_validation.permissions.yml
@@ -0,0 +1,4 @@
Expand All @@ -76,7 +112,7 @@ index 0000000..579ecaa
+ restrict access: TRUE
diff --git a/clientside_validation.routing.yml b/clientside_validation.routing.yml
new file mode 100644
index 0000000..9cf0f16
index 0000000000000000000000000000000000000000..9cf0f16a5754e254d777a758f9f628c30746b8b5
--- /dev/null
+++ b/clientside_validation.routing.yml
@@ -0,0 +1,7 @@
Expand All @@ -89,36 +125,36 @@ index 0000000..9cf0f16
+ _permission: 'administer clientside validation'
diff --git a/config/install/clientside_validation.settings.yml b/config/install/clientside_validation.settings.yml
new file mode 100644
index 0000000..dd9fdb9
index 0000000000000000000000000000000000000000..1a8ba2232f7b80975fd88f08fa42a9c657386c3a
--- /dev/null
+++ b/config/install/clientside_validation.settings.yml
@@ -0,0 +1,2 @@
+enable_all_forms: true
+enabled_forms: { }
+validation_scope: 0
+selected_forms: { }
diff --git a/config/schema/clientside_validation.schema.yml b/config/schema/clientside_validation.schema.yml
new file mode 100644
index 0000000..6f8ec51
index 0000000000000000000000000000000000000000..2091d20506888839fe13f7a48b3241387fb24785
--- /dev/null
+++ b/config/schema/clientside_validation.schema.yml
@@ -0,0 +1,12 @@
+clientside_validation.settings:
+ type: config_object
+ mapping:
+ enable_all_forms:
+ type: boolean
+ label: 'Setting to enable all forms for clientside validation'
+ enabled_forms:
+ validation_scope:
+ type: integer
+ label: 'Setting for the validation scheme'
+ selected_forms:
+ type: sequence
+ label: 'A list of clientside validation enabled forms'
+ label: 'A list of forms, based on the validation scheme'
+ sequence:
+ type: string
+ label: 'The form ID'
diff --git a/src/Form/ClientsideValidationSettingsForm.php b/src/Form/ClientsideValidationSettingsForm.php
new file mode 100644
index 0000000..36b2547
index 0000000000000000000000000000000000000000..25ede1e222fc8b1d4186e6a9349f6eac89e2b26c
--- /dev/null
+++ b/src/Form/ClientsideValidationSettingsForm.php
@@ -0,0 +1,98 @@
@@ -0,0 +1,103 @@
+<?php
+
+namespace Drupal\clientside_validation\Form;
Expand Down Expand Up @@ -175,28 +211,33 @@ index 0000000..36b2547
+ '#markup' => $this->t('Forms with the "novalidate" attribute will <strong>not</strong> have clientside validation enabled, regardless of these settings.'),
+ ];
+
+ // General enabling for all forms.
+ $form['enable_all_forms'] = [
+ '#type' => 'checkbox',
+ '#title' => $this->t('Use Clientside Validation in all forms'),
+ '#description' => $this->t('Enable Clientside Validation for all forms on this site.'),
+ '#default_value' => $config->get('enable_all_forms'),
+ // Set the scope of the validation.
+ $form['validation_scope'] = [
+ '#type' => 'radios',
+ '#title' => $this->t('Select the forms to validate'),
+ '#default_value' => $config->get('validation_scope') ?: '0',
+ '#options' => [
+ '0' => $this->t('Validate all forms.'),
+ '1' => $this->t('Only validate forms listed below.'),
+ '2' => $this->t('Validate all forms except those listed below.'),
+ ],
+ ];
+
+ // Enabled forms.
+ $enabled_forms = (!empty($config->get('enabled_forms'))) ? $config->get('enabled_forms') : [];
+ $form['enabled_forms'] = [
+ // Selected forms.
+ $selected_forms = (!empty($config->get('selected_forms'))) ? $config->get('selected_forms') : [];
+ $form['selected_forms'] = [
+ '#type' => 'textarea',
+ '#title' => $this->t('Clientside Validation Enabled Forms'),
+ '#description' => $this->t('Enter form IDs for all forms that should have clientside validation enabled, separated by a new line.'),
+ '#default_value' => implode(PHP_EOL, $enabled_forms),
+ '#title' => $this->t('Enter form IDs below, separated by a new line.'),
+ '#default_value' => implode(PHP_EOL, $selected_forms),
+ '#states' => [
+ // Hide this textarea when all forms are enabled.
+ 'invisible' => [
+ 'input[name="enable_all_forms"]' => ['checked' => TRUE],
+ 'visible' => [
+ // Only show this when validating less than all.
+ [':input[name="validation_scope"]' => ['value' => '1']],
+ 'or',
+ [':input[name="validation_scope"]' => ['value' => '2']],
+ ],
+ ],
+ ],
+ ];
+ ];
+
+ return $form;
+ }
Expand All @@ -209,10 +250,10 @@ index 0000000..36b2547
+ $config = $this->config('clientside_validation.settings');
+ $values = $form_state->getValues();
+
+ $config->set('enable_all_forms', $values['enable_all_forms']);
+ $enabled_forms = preg_split("[\n|\r]", $values['enabled_forms']);
+ $enabled_forms = array_filter($enabled_forms);
+ $config->set('enabled_forms', $enabled_forms);
+ $config->set('validation_scope', $values['validation_scope']);
+ $selected_forms = preg_split("[\n|\r]", $values['selected_forms']);
+ $selected_forms = array_filter($selected_forms);
+ $config->set('selected_forms', $selected_forms);
+ $config->save();
+ }
+
Expand Down

0 comments on commit e5aea27

Please sign in to comment.