views:

249

answers:

3

Can someone help me render a form and table on the same page? I'm sure it's easy, but can't think of how to do it.

Here's hook_menu:

 function ncbi_subsites_menu() {
        $items = array();

        $items['admin/content/ncbi_subsites'] = array(
            'title' => 'NCBI Subsites Module',
            'description' => 'Informs Drupal about NCBI subsites as defined by the Content Inventory database',
            'page callback' => 'ncbi_subsites_show_main_page',
            'access arguments' => array( 'administer site configuration' ),
            'type' => MENU_NORMAL_ITEM,
        );
        return $items;
}

Here's the callback:

function ncbi_subsites_show_main_page() {
    $subsites = ncbi_subsites_get_subsites_from_inventory(); // fnc returns associative array from inventory, defined in include
    return ncbi_subsites_make_table( $subsites );
}

In the callback I call some helper functions that return a themed, paged table.

What I want is a small form above the table. How would I that?

+1  A: 
function ncbi_subsites_show_main_page() {  
    $subsites = ncbi_subsites_get_subsites_from_inventory(); 
    $page = drupal_get_form('your_form');  
    $page .= ncbi_subsites_make_table( $subsites );  
    return $page;  
}
Michael D
A: 

Hmm, when I try this:

$form['force-update'] = array( 
    '#description' => 'Force a read from inventory database to get subsites',
    '#type' => 'submit',
    '#value' => 'Update Subsites',
    '#submit' => array( 'ncbi_subsites_update_handler'),
);
$output.= drupal_get_form('force-update' );

I get this error:

warning: call_user_func_array(): First argument is expected to be a valid callback, 'force-update' was given in /export/home/web/private/htdocs/cms-dev/includes/form.inc on line 371.

I thought drupal_get_form takes the id of the form, and the #submit value is the callback...

Aaron
A callback is a function, so put your form in side 'function force-update() {...}' - and better not to ask more questions in the 'your answer' box, or it leaves answers in the comment section and doesn't really work.
lazysoundsystem
Sorry about that. I've found in the past (and seen others in this forums saying the same) that comments often don't get read. But I see your point.
Aaron
A: 

Hmm again. I figured it out, but don't quite understand it.

If I pass the name of the function that returns the form array to drupal_get_form, it works:

$output = drupal_get_form( 'ncbi_subsites_form');

But the docs say that drupal_get_form takes the id of the form, not the function that returns the form. What's up with that?

Aaron
Check out http://api.drupal.org/api/function/drupal_get_form/6 "$form_id The unique string identifying the desired form. If a function with that name exists, it is called to build the form array. Modules that need to generate the same form (or very similar forms) using different $form_ids can implement hook_forms(), which maps different $form_id values to the proper form constructor function."
bkildow
Ahh, I missed that line for some reason. Thanks for the clarification
Aaron