I have been working on this all day, and finally have something working - but I feel there must be a better way. I have a page that shows a list of node titles, and a small form on that page of taxonomy select drop downs to filter the nodes by (basically).

If a typical page callback has other dynamic content, and then also shows a form by use of drupal_get_form - what's the best way to get data back to that page callback function?

All the form examples are using drupal_get_form as the page callback function. And those that use form_submit are just setting a drupal message, but what if you need to use that posted form data on a larger page? The only way I could finally figure out was to use variable_set() and variable_get(). But is there a way to send data to the page without writing to the db?

Comments

rfay’s picture

Category: feature » support
Status: Active » Fixed

Sure -

A page is just a render array.

$page[] = drupal_get_form(my_form).
...
return $page;

wheelercreek’s picture

I'm not really sure how that helps me.. here's more info about the module I've written:

/**
 * Implementation of hook_menu().
 */
function wpapers_menu() {
	$items['white-papers'] = array(
		'title' => 'White Papers',
		'access callback' => true,
		'page callback' => 'wpapers_list',
		'type' => MENU_NORMAL_ITEM,
	);
	return $items;
}

/**
 *  Implementation of hook_theme().
 */
function wpapers_theme($existing, $type, $theme, $path) {
	$hooks['wpapers_list'] = array(
		'template' => 'wpapers-list',
	);
	return $hooks;
}
/**
*	Page callback
**/
function wpapers_list() {
	//shows the form at the top (select menus) to filter the list of white papers
        // by means of  drupal_get_form() & loading that form into a variable so it 
        //can be printed in the .tpl file.
        //also shows list of selected white papers
        ...
        $data['content'] = ...the list of selected papers
        $data['filters'] = drupal_get_form('wpapers_filter_form');
        return theme('wpapers_list', $data); 
}

function wpapers_filter_form_submit($form, &$form_state){
	variable_set('white_paper_ind', $form_state['values']['industry']);
	variable_set('white_paper_prod', $form_state['values']['product']);
}

function wpapers_filter_form($form, &$form_state){
  //builds a form with select menus to filter the list of white papers by taxonomy terms 
  //uses variable_get to pre-select the filters.
}

How can I do this without storing to the db? I've even tried accessing the $_POST directly, but somewhere in the process it's getting blown away or overwritten...

rfay’s picture

Sorry, it's not clear to me what you want to do.

You're almost doing the page callback right, but in D7 you would leave the page as a render array and let it be themed later. You could set #theme on it. See the render_example.

And sorry, this isn't a support venue. You'll need to spread out and get support in IRC, on drupal.stackexchange, or wherever.

dave reid’s picture

Version: » 7.x-1.x-dev
wheelercreek’s picture

Well - I'm just looking for help understanding how the passing of data is supposed to work with forms-to-pages.. maybe this isn't the right place...

The page callback is working fine, my form appears on the page along with the list of content. When I submit the form, the submit function runs, great - but how do I get data from that submit function back to to the page callback function to use it to filter my query.. without having to first store it in the db?

There doesn't seem to be a way to use form posted data on the page. All the form examples basically just print a message.

rfay’s picture

Take a look at the core search module and several other places this is done.

What they do is turn the form submission arguments into a new GET, and then build the page based on the GET.

So if your form contains $form_state['status']['state'] = 'co'

then in your submit handler, $form_state['redirect'] = 'my_page/co'

And then that uses the arg to build the page.

wheelercreek’s picture

perfect, thanks.. that thought hit me this morning too - so a page redirect is the key, and then use the menu system with wildcard.. that should work! thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.