I'm a bit lost trying to create a page with hook_menu that displays a form and a table below it. Preferably the table should only be displayed after the form has been submitted.

I don't have any issue with displaying the table or the form, but I can't get it to be displayed at the same time on the same page.

I have a my_module_table() function that ends with

<?php
...
return
theme('table', array( 'header' => $header'rows'=> $rows  ) );
?>

I then have a function for the form my_module_form($form, &$form_state)

I then have this function to put both of these functions in an array

<?php
function my_module_page() {
 
$output = array();
 
$output['form'] = drupal_get_form('my_module_form');
 
$output['table'] = 'my_module_table';
  return
$output;
}
?>

Than last I try to create the page and get both the form and the table to render on the page using hook_menu

<?php
function my_module_menu() {
 
$items = array();

 
$items['mymodule'] = array(
   
'title' => 'My module',
   
'access callback' => TRUE,
   
'page callback' => 'mymodule_page',
  );
  return
$items;
}
?>

I don't have any issues with the form or table functions, they work fine if I don't try to render both at ones.

Can anybody tel me what I'm doing wrong?

Comments

Jaypan’s picture

This:

<?php
  $output
['table'] = 'my_module_table';
?>

Should be this:

<?php
  $output
['table'] = my_module_table();
?>

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

Jaypan’s picture

Also note that this:

<?php
...
return
theme('table', array( 'header' => $header'rows'=> $rows  ) );
?>

Can be this:

<?php
...
return array (
 
'#theme' => 'table',
 
'#header' => $header,
 
'#rows' => $rows,
 
'#empty' => t('Nothing found. Nada. Zip. Zilch.'),
);
?>

This will add this fragment to the render array you are creating in my_module_page(), meaning it can be overridden further down the line if necessary.

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

einsiol’s picture

Thank you for that, that's something I will try out. I have already resolved the issue with the help of stackexchange.com but I will try out this solution when I have the time to see if it works to. I will post the answer from durpa.stackexchange.com in a separate comment.

Einar Ólafsson, einland.is

einsiol’s picture

You where right, this worked better then the solution in drupal.stackexchange.com, I'm going to use your solution. If you have the time you should post this solution on drupal.stackexchange.com so I can choose it as the preferred answer.

Einar Ólafsson, einland.is

Jaypan’s picture

Done.

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

einsiol’s picture

I ask this question also on drupal.stackexchange.com and got an answer that resolved my issue

http://drupal.stackexchange.com/questions/89321/hook-menu-display-a-form...

Einar Ólafsson, einland.is

Jaypan’s picture

Well, the difference is that on Stack Overflow, they showed you how to display a table as part of the form. What I've shown you here is how to display a table as well as a form on a page. It may be that either of these suits your needs, it may be that one suits your needs over the other. Pick and choose as necessary.

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

einsiol’s picture

I will probably need to use the solution you suggested, if not now in this project then at least later.

Einar Ólafsson, einland.is