Hy,

I am currently converting a simple search module I created for a drupal site to the 4.7 core, but i am experiencing some problems.

My old module had a search page that consisted of two parts:
- The search form
- The results of the search, when a search was done.

The classic way was to implement a callback menu function

function pletdb_menu(){
	global $user;
	$items = array();

	$items[] = array('path' => 'pletdb/search','title' => t('Custom search'),'callback' => 'pletdb_custom_search', 'access' => user_access('search pletdb'), 'type' =>MENU_CALLBACK);
	return $items;
}

Then I had the function


function pletdb_custom_search(){

// Code that creates the form elements, ending with the form() function
$output = form($form)
// ... setting the variables for later processing
$op = isset($_POST['op']) ? $_POST['op'] : '';
$edit = isset($_POST['edit']) ? $_POST['edit'] : '';
// ... execute if the form is submitted ...
if ($op == t('Submit')) {
 // ... Create a query with the elements that are posted on the form ...
$result = db_query($query);
// ... proces the $result and format into a nice table (html string in $result_table variable)
$output .= $result_table
}

Now, how can I do this with the new form api. Can someone describes a best practice how I can create a form and display the output of my search underneath?

I understand that in the new form api, the form is validated/executed/created by the drupal_get_form() function, that ends with a drupal_goto() function (and I loose the $_post variables, and every variable I have set before calling drupal_get_form()). But how can i preserve the form variables in order to generate extra output :)
Looks like a beginning php form .... :)

Hope that someone can help me with this!

Comments

jsloan’s picture

... to supress the redirect add this declaration to your $form[]

$form['#redirect'] = FALSE;

... build your form

$page = drupal_get_form('your_form', $form);

... now test for the existence of the posted $form_values[]

if ($form_values['searchText']){
  // insert code to process the form variables and add it to the $page
  $page .= some_function($your_form['field_name']);
}

... here is a snippet from a working search form I use:


function forms_search_form() {
 global $form_values;

  $form["searchText"] = array(
    '#type' => 'textfield',
    '#title' => t("Look for"),
    '#size' => 30,
    '#maxlength' => 255,
  );
  $form['action'] = array(
    '#type' => 'submit',
    '#value' => t('Submit Search')
  );
  $form['#redirect'] = FALSE;

  $page = drupal_get_form('form_search', $form);

  if ($form_values['searchText']){   
    $page .= _search($form_values['searchText']);
  }

  return $page;
}


dman’s picture

What's actually going on with that redirect? Why is it used?

The flowchart helps a little bit, and I think I can see what's going on enough for my own forms, but I was having trouble when adding content fields to my nodes like the old forms API used to.

The goto seems to be bugging me because I've got some debug output going on :-/ and I can't get my head around where the data (and form structure) is being saved in between submission and save if the request is being bounced.

I think I've just missed a concept somewhere.

.dan.

http://www.coders.co.nz/

jsloan’s picture

It also confused me

I thought that I would have to copy the form array into a session variable but supressing the redirect solved that problem. The form array is available in $form_values but since it is not passed back to the form function I declared it as a global so I could access the array.

moshe weitzman’s picture

fyi, that redirect code yesterday today in HEAD. the redirect happens after the subit handlers have been called. if you have devel.module turned on, you can stop redirects for a moment and look at denug output and the query log. see admin/settings/devel

twom’s picture

Thanks for the reply. It's exactely what I needed :)

regards,

Tom

Hubert_r’s picture

That's also just what I am looking for (i.e display a form, then on submit display the form again, with some additional content below the form)

However $form_values doesn't exist when the form gets submitted. (I made sure it was declared as global)

Any idea why?

reed.r’s picture

Thankx for the "$form['#redirect'] = FALSE;" solved my problems in a whiff.

Seems like folks wonder where to get the data; look in $_POST['edit']

$edit = $_POST['edit'];
echo $edit['field_name'];

Worked for me.

adamtyoung’s picture

Thanks so much for your solution, but there is something that is still confusing me. I am building a module and need a page that will handle its own submitted results. So, I have my form, my hook_menu entry for the page, and the callback from hook_menu (function that is called when the url is hit). This line from your explanation is confusing me:

$page = drupal_get_form('form_search', $form);

Isn't this line just calling the form that it is nested in? Is this calling a different form?

Should I include everything after the $page var in my callback function and not in the form function itself? Thanks in advance for any help,

Adam Y

Adam Young
Vancouver, BC
http://www.adamtyoung.ca

inforeto’s picture

Thanks, i was wondering why the post was turning into get and suspected something was missing.
This topic was a bit hard to find in the search results, due to the words post and method being too common.

wickedskaman’s picture

It's always the little things... this thread helped me too! Thanks posters!

Steve Ramos
www.zapcrunch.com