I have a custom-generated form on a page and it needs to submit data to a view on a different page. I'm currently trying to get this to work by passing the form #validate through a submit hook and then using $form_state['redirect'] = 'customsearch/'; to catch the results. The view has several exposed filters and will ideally set themselves to the data received from the initial custom form page.

It doesn't look as though data is actually making it to the /customsearch/ landing page however. I have enabled the Views Expost module and am watching for POST variables from a PHP field in the landing page view.

The current code including form, validation and redirection can be found over here.

What is the proper 'Drupal' way of passing data from a form to a view with exposed filters? Should I be watching for GET or POST variables or will Drupal pass data with a different set of functions than the ones currently used?

up vote 1 down vote accepted

Views exposed filters are using information supplied by the relevant GET parameters so this use case is actually very simple. You only need to build the correct url in your custom form submit handler from the received data and then redirect the user. A basic example to illustrate this:

function mymodule_custom_search_form($form, &$form_state) {
  $form['key1'] = array(
    '#title' => t('Key1'),
    '#type' => 'textfield',
  );
  $form['key2'] = array(
    '#title' => t('Key2'),
    '#type' => 'textfield',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );

  return $form;
}

function mymodule_custom_search_form_submit($form, &$form_state) {
  $options = array('absolute' => TRUE);
  $key1 = trim($form_state['values']['key1']);
  if ($key1) {
    $options['query']['key1'] = $key1;
  }
  $key2 = trim($form_state['values']['key2']);
  if ($key2) {
    $options['query']['key2'] = $key2;
  }
  $form_state['redirect'] = url('customsearch', $options);
}

If the user submits the form with "key1" value "one" and "key2" value "two" then they will be redirected to "customsearch?key1=one&key2=two". What you need to note is that the GET parameters "key1" and "key2" must match with what you've set in the relevant view: click on your filter criteria -> "MORE" link at the bottom and then check/modify "Filter identifier".

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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