2

I have created a module that creates and displays a table and with a filter element. What I would like to do is when the user enter a string into the filter can submits it, I then use that string to filter the results of the table. What I am having an issue with is how to pass that string back into the form creation function to filter the results.

This is what I have in the submit function:

function submission_list_form_submit($form, &$form_state) {

  $filter = array();
  if (isset($form_state['values']['filter_user'])) {
    $filter['username'] = $form_state['values']['filter_user'];
  }
  return $filter;
}

Which is not right obviously. How do I get the $filter variable to be passed back into the submission_list_form function that creates the page? This is being done with Drupal 7.

Thanks.

3 Answers 3

5

You can use the following code, to achieve what you want.

function submission_list_form_submit($form, &$form_state) {
  $filter = array();

  if (isset($form_state['values']['filter_user'])) {
    $form_state['filter']['username'] = $form_state['values']['filter_user'];
  }

  $form_state['rebuild'] = TRUE;  
}

The form building function should then check if $form_state['filter'] is set.

function submission_list_form($form, &$form_state) {
  if (isset($form_state['filter'])) {
    // …
  }

  // …

  return $form;
}

If the code doesn't work, use another array index instead of $form_state['filter']; it is probable that in such case the array index conflicts with the one used by Drupal or a third-party module. I would rather prefix the array index with the short module name, to be sure to avoid conflicts with other modules or future Drupal versions.

References

1
  • Thank-you that worked perfectly. And good point about prefixing the index array to avoid name collisions.
    – user5013
    Commented Aug 9, 2013 at 15:58
0

In submission_list_form_submit(), instead of returning $filter, you can set this value on the $_GET variable, and then get this value back when you're in your form building function.

1
  • Form submission handlers and form builders are not hooks. Furthermore, hook_form() is a not a form builder, since it gets different parameters.
    – avpaderno
    Commented Aug 9, 2013 at 10:02
-1

I'm not sure but I think you should use variable_get and set to pass it. Hope that helps.

1
  • It doesn't work, since the value returned from variable_get() is the same for all the users. What would happen if more than one user is using that form? Also, answers should not be guesses; if you are not sure, and you think it should work, it would be better to write a comment, which you are not still allowed to write for questions asked by others.
    – avpaderno
    Commented Aug 9, 2013 at 10:04

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.