0

I have a third-party vender that provides particular information for us when queried.

Basically, we need to change the URL of the search results page so that their supplied javascript can contact their site and return results from searching the same term.

So if I searched for polar bears the resulting path would normally look like this http://foo.com/search/node/polar%20bears

I need to append to the path like this http://foo.com/search/node/polar%20bears?scq=polar%20bears or http://foo.com/search/node/polar%20bears?scq=polar bears

How would I accomplish this?

2 Answers 2

1

When you want to alter the behavior of an existing form, hook_form_alter() is a useful tool here. Here you want to alter the existing form so that the redirected URL has an extra query parameter. By looking at the submission handler we can see that the search form uses drupal_redirect_form() to go to the /search/node/SEARCH_TERM path.

We can add an extra submission handler to this search form so that it modifies the redirect path with the extra query parameter.

Here's an example MYMODULE that would do as such for the search block form.

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  // Add an extra submission handler the search block form.
  if ($form_id == 'search_block_form') {
    $form['#submit'][] = 'MYMODULE_search_box_form_submit';
  }
}

/**
 * search_block_form submission handler
 */
function MYMODULE_search_box_form_submit($form, &$form_state) {
  $form_state['redirect'] = array($form_state['redirect'], array(
    'query' => array(
      'scq' => $form_state['values']['search_block_form'],
    )
  ));
}
1
  • Thank you, Shawn. I was able to use your info and see the direction things needed to go and revise for my needs. I appreciate your great explanation and help. This is how I wish most people would help, given the time. Two thumbs up! \0/
    – Cy Jobes
    Apr 28, 2016 at 13:50
0

I just wanted to share my final code. It is similar to the answer given by Shawn Conn, but revised a bit to use the correct array elements.

    <?php

/**
 * @file
 * Adds a submit handler to the default search, then appends the redirect path to allow for third-party
 * javascript to gather search data based on the $_GET['scq'] variable value
 */


/**
 * Implements hook_form_alter().
 */
function My_module_form_alter(&$form, &$form_state, $form_id) {
    // Add an extra submission handler to the form with matching $form_id
    if ($form_id == 'search_block_form' || $form_id == 'search_form') {
        $form['#submit'][] = 'My_module_search_box_form_submit';
    }
}

/**
 * search_block_form submission handler
 */
function My_module_search_box_form_submit($form, &$form_state) {
    // Get the $form_id
    $form_id = $form['#form_id'];

    // Gather the terms used in the search from the appropriate search
    if ($form_id == 'search_form') {
        $terms = $form_state['input']['keys'];
    }
    elseif ($form_id == 'search_block_form') {
        $terms = $form_state['values'][$form_id];
    }

    // Build the correct redirect path, appending the third-party variable (scq) with $terms as the value
    $form_state['redirect'] = array(
        $form_state['redirect'],
        array(
        'query' => array(
            'scq' => $terms,
        )
    ));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.