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

...
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

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

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:

  $output['table'] = 'my_module_table';

Should be this:

  $output['table'] = my_module_table();

The Drupal organization has shut down discussion on improvement of the forums: https://www.drupal.org/node/2536122

It's time to start a new forum somewhere else. The Drupal organization does not care about the forums.

Jaypan’s picture

Also note that this:

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

Can be this:

...
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.

The Drupal organization has shut down discussion on improvement of the forums: https://www.drupal.org/node/2536122

It's time to start a new forum somewhere else. The Drupal organization does not care about the forums.

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.

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.

Jaypan’s picture

Done.

The Drupal organization has shut down discussion on improvement of the forums: https://www.drupal.org/node/2536122

It's time to start a new forum somewhere else. The Drupal organization does not care about the forums.

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...

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.

The Drupal organization has shut down discussion on improvement of the forums: https://www.drupal.org/node/2536122

It's time to start a new forum somewhere else. The Drupal organization does not care about the forums.

einsiol’s picture

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