Allow additional querystring parameters in config form

Created on 4 December 2023, 7 months ago

Problem/Motivation

We are using Google Tag Manager to track 404 redirects. Our Analytics team uses specific querystrings to track the types of redirects and the sorts of events that trigger them. The querystrings do quite a bit of work for the team and are a simple and elegant solution for the work they need to do.

Currently, the webform element for Search404 Custom Search Path does not allow the "&" symbol.

Proposed resolution

Currently, we are hijacking the search404 settings form with form_alter and form_validate funcitons:



/**
 * Implements hook_form_alter().
 */
function ourmodule_search_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Check if the form is the Search404Settings form.
  if ($form_id === 'search404_settings') {
    // Add a custom validation function.
    $form['#validate'] = ['ourmodule_search_search404_settings_form_validate'];
  }
}

/**
 * Custom validation function for the Search404Settings form.
 */
function ourmodule_search_search404_settings_form_validate(array &$form, FormStateInterface $form_state) {
  // Validation for custom search path.
  if (!empty($form_state->getValue('search404_do_custom_search'))) {
    $customPath = $form_state->getValue('search404_custom_search_path');

    if (empty(preg_match("/@keys$/", $customPath))) {
      $form_state->setErrorByName('search404_custom_search_path', t('Custom search path should end with search key pattern "@keys".'));
    }

    // Split the custom path at "@keys".
    $urlParts = explode('@keys', $customPath);
    // Validate the characters before the "@keys" part.
    if (!preg_match('/^[a-zA-Z0-9\/?=_&]+$/', $urlParts[0])) {
      $invalidCharacters = preg_replace('/[a-zA-Z0-9\/?=_&]/', '', $urlParts[0]);
      $form_state->setErrorByName('search404_custom_search_path', t('Characters before "@keys" should only contain alphanumeric characters, "/", "?", "=", "_", and "&". Invalid characters found: @invalid', ['@invalid' => $invalidCharacters]));
    }

    if (strpos($customPath, '/') === 0) {
      $form_state->setErrorByName('search404_custom_search_path', t('Custom search path should not start with a slash.'));
    }
  }
}

/**
 * Implements hook_preprocess_HOOK() for page templates.
 */
function ourmodule_search_preprocess_page(&$variables) {
  // Check if the query string contains 'redir=y'.
  $query = \Drupal::request()->query;
  if ($query->has('redir') && $query->get('redir') == '404') {
    // Set the is_redirect variable to true.
    $variables['is_redirect'] = true;
  } else {
    // Set the is_redirect variable to false.
    $variables['is_redirect'] = false;
  }
}

This doesn't seem to be having an adverse affect on how the module works at all. As long as the validation for the last part of the querystring remains the search terms, I'm not seeing any issue at all with this.

✨ Feature request
Status

Closed: duplicate

Version

2.1

Component

User interface

Created by

πŸ‡ΊπŸ‡ΈUnited States loopy1492

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024