4
votes

I am altering the value of the form action for the basic search form on a Drupal site. Here is the line I added to our hook_form_alter() implementation in our custom search module:

$form_id['#action'] = 'http://mydomain.com/search/customsearchmodule/';

When I view the source for the form, the action is exactly as I have set it in the above line. The problem is, after the form has been submitted, the default search results page is displayed, not the custom search results page.

How do I set the custom search results page to display, rather than the default one?

Edit

Here is an example of my hook_form_alter() implementation:

/**
* Implementation of hook_form_alter()
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'search_block_form') {
    $form['#action'] = 'http://www.mydomain.com/search/mymodule/';
  }
}

Also:

I added unset($form['#submit']); to my implementation of hook_form_alter() and, after submitting the form, arrived at the appropriate URL (http://www.mydomain.com/search/mymodule), only without any data passed (no keywords).

Edit #2

I am digging around in the Drupal API and in the core search module just to see how it works. This is the core search_box_form_submit() function from our /modules/search/search.module:

    /**
     * Process a block search form submission.
     */
    function search_box_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($_REQUEST['destination'])) {
        unset($_REQUEST['destination']);
      }
      if (isset($_REQUEST['edit']['destination'])) {
        unset($_REQUEST['edit']['destination']);
      }

      $form_id = $form['form_id']['#value'];
      $form_state['redirect'] = 'search/node/'. trim($form_state['values'][$form_id]);
    }

Note the line where $form_state['redirect'] is defined. If I comment this line out, then the search form will land on the search/mymodule/ page, although without any search keywords passed.

Edit - Working Code

The final solution is an altered version of nmc's answer. I had to alter his mymodule_form_submit() implementation. The function should be named mymodule_submit() and the code within the function needed to be altered as well:

function mymodule_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.'));
  }
  else
  {
    $form_id = $form['form_id']['#value'];
    $form_state['redirect'] = 'search/mymodule/'. trim($form_state['values'][$form_id]);
  }
}
2
Are you performing custom code for the actual search or do you just want to have a customized search results page?Laxman13
I have implemented hook_search() in the module, for a custom search. So, yes, it is more than just a custom search results page.Mike Moore
Not sure, that sounds strange. Not sure if you meant search instead of searcy but I'm sure that was just a typo here and not the root of the problem. You could always ask at drupal.stackexchange.comLaxman13
Whoops that was just a typo here. I meant search, not searcy. Fixing now. Thanks for the URL to the drupal stackexchange, I'll give that a shot.Mike Moore
Why do you use this core form, if you choose to override all its workings? Have you considered creating your own search-box? If not, why not?berkes

2 Answers

7
votes

Try changing the submit function which the form calls, instead of changing the URL of the form's action:

/**
* 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]);
  }
  else {
    form_set_error(NULL, t('Search is currently disabled.'), 'error');
  }
}

This should redirect the search to http://www.mydomain.com/search/mymodule/keywords which your module can then process the keywords accordingly.

0
votes

I believe you wanted to update #action element of $form, not $form_id?

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_form') {
    $form['#action'] = 'MYMODULE_search_url';
  }
}

works perfectly fine, takes me to my own page and does not display standard search results there.

Edit:

Ok, I have played with it a little bit more now, and actually what I have written above works in 33,(3)%. ;) Trying it with Garland theme, I have one search box in left sidebar ($form_id = 'search_theme_form') - let's call it Search #1, one search box on /search page (Search #2) and third one on /search/node/whatever results page (Search #3).

Now, what I've just seen is that updating $form['#action'] works in case of Search #2, but do not work in two other cases. Therefore, solution not satisfactory, let's try a different approach then.

Let's add our own #submit function to form in question:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_form' || $form_id == 'search_theme_form') {
    $form['#submit'][] = 'MYMODULE_submit';
  }
}

function MYMODULE_submit($form, &$form_state) {
  $form_state['redirect'] = 'MYMODULE_path';
}

And voila, works in all 3 cases now!