2

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, but that's not the main issue.

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?

1
  • Instead of getting the table data into the output array as $output['table'], why not get the table into the form as a markup element and displaying the form as such? Oct 18, 2013 at 7:25

3 Answers 3

1

You can change this:

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

To this:

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

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.

0
2

Here's an implementation of my earlier suggestion to append the table as a form markup and rendering the form with table in the menu callback

/**
 * Form builder function
 */
function mymodule_form() {
  $form = array();
  $form['sometext'] = array(
    '#type' => 'textfield',
    '#title' => t('Some random textfield'),
    '#default_value' => 'Hello world',
  );
  $form['tablemarkup'] = array(
    '#markup' => my_module_table(),
  );
  return system_settings_form($form);
}


/**
 * Callback function for returning form with themed table
 */
function mymodule_page() {
  $output = array();
  $form_data = drupal_get_form('my_module_form');
  $output = drupal_render($form_data);
  return $output;
}

/**
 * Function to return themed table.
 */
function my_module_table() {
  $header = array(
    'data' => 'Table title',
  );
  $rows[] = array(
    'Table data value',
  );
  return theme('table', array('header' => $header, 'rows'=> $rows));
}
1
  • Thank you so much for taking the time and give me this solution, it helped me work out this issue :) You helped me keep myself sane for little longer :) Oct 18, 2013 at 9:52
0

You should convert your output to a renderable array for starters, but you could try something like

$output = render(drupal_get_form('my_module_form'));
$output .= my_module_table();

i think it will work, edited.

3
  • It won't - drupal_get_form() returns an array...
    – Clive
    Oct 18, 2013 at 8:09
  • @Clive correct. - edited
    – Pan Chrono
    Oct 18, 2013 at 9:38
  • 1
    I'm just being picky now, but you should do it like this: $form = drupal_get_form('my_module_form'); $output = drupal_render($form);, otherwise you'll get warnings about passing by reference
    – Clive
    Oct 18, 2013 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.