0

I would like to modify the search box to point to a custom search function. So far, I have modified template.php to detect when the search box is being called from a specific module and I reset some variables.

function my_theme_preprocess_search_block_form(&$vars, $hook) {
    $uri = request_uri();
    if (preg_match("/\/my_module/", $uri)) {
        unset($vars["form"]["search_block_form"]["#title"]);
        $vars["form"]["search_block_form"]["#title"] = t("SOME SEARCH MESSAGE");
        $vars["form"]["search_block_form"]["#value"] = t("SOME SEARCH TERM");
    }
}

That works fine. But now what I want to do is have that form submit to some module of my own creation. Oddly, this isn't as obvious. Do I need to modify something in modules/search/ ? If so, what?

2
  • are you not using core search at all at this point? If so, why not just create a custom block with your search form in there and disable core search for performance gain. That way you don't even need to work with the form itself..
    – schnippy
    Commented Dec 9, 2013 at 19:23
  • No, I only want to modify core search for this one module. This one module essentially proxies content from another system so in order for search to be meaningful I need to also proxy searches. Commented Dec 9, 2013 at 19:27

1 Answer 1

0

The way to alter the action handler for the search form is to add a submit handler, as outlined here: https://stackoverflow.com/questions/6444769/altering-search-form-action-not-working-for-custom-search-module-drupal-6. You can call your function in the submit handler or redirect to it from the submit handler.

/**
* Implementation of hook_form_alter()
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'search_block_form') {
    $form['#submit'][] = 'mymodule_form_submit';
  }
}

/**
 * Process a block search form submission.
 */
function mymodule_form_submit($form, &$form_state) {
  // The search form relies on control of the redirect destination for its
  // functionality, so we override any static destination set in the request,
  // for example by drupal_access_denied() or drupal_not_found()
  // (see http://drupal.org/node/292565).
  if (isset($_GET['destination'])) {
    unset($_GET['destination']);
  }

  // Check to see if the form was submitted empty.
  // If it is empty, display an error message.
  // (This method is used instead of setting #required to TRUE for this field
  // because that results in a confusing error message.  It would say a plain
  // "field is required" because the search keywords field has no title.
  // The error message would also complain about a missing #title field.)
  if ($form_state['values']['search_block_form'] == '') {
    form_set_error('keys', t('Please enter some keywords.'));
  }

  $form_id = $form['form_id']['#value'];
  $form_state['redirect'] = 'search/mymodule/'. trim($form_state['values'][$form_id]);
  }
1
  • Great! This is what I needed. An important thing to note, though, is that unset($form['#submit']); is necessary before $form['#submit'][] = 'mymodule_form_submit'; in mymodule_form_alter. Commented Dec 9, 2013 at 20:18

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.