1

I have created a block_view, In that block I want to show dynamic table.

I have written following code.

$table_headers = array(t('User'), t('Relation'),t('Period') );
foreach ($result as $row) {
            $table_rows[] = array(
            $row->name,
            $row->relation,
            $row->period_from,
           );
         }
 dpm($table_rows); 

In above $table_rows array, I am getting following elements

enter image description here

If I am using

$table_html = theme_table($table_headers,$table_rows);
$block['content'] = $table_html;

then getting following error on page

The website encountered an unexpected error. Please try again later.

If I used

$table_html = theme('table',$table_headers,$table_rows);
dpm($table_html); 
$block['content'] = $table_html;

I am getting following html on page

enter image description here

Please suggest were I made mistake and what should I do.

1

The error is how you pass the parameters to the theme hook. You should use the following code.

$block['content'] = theme('table', array('header' => $table_headers, 'rows' => $table_rows));

In Drupal 7, theme hooks receive their parameters in an array.

You could also use the following code, since on Drupal 7 you can directly use a render array.

$block['content'] = array(
  '#theme' => 'table',
  '#header' => $table_headers,
  '#rows' => $table_rows
);

If a module modified the table theme hook, and you don't understand which variables the modified version uses, or in general, if you don't know which variables are used from a theme hook, you can print the value returned from theme_get_registry() to check that.
For example, in my test site, drush ev "\$hooks = theme_get_registry(); var_export(\$hooks['table']);" prints me the following.

array (
  'variables' => array(
    'header' => NULL,
    'rows' => NULL,
    'attributes' => array(),
    'caption' => NULL,
    'colgroups' => array(),
    'sticky' => true,
    'empty' => '',
  ),
  'type' => 'module',
  'theme path' => 'modules/system',
  'function' => 'theme_table',
) 
  • this is not in drupal's theme_table, can you suggest me other documentation ? – Devidas Kadam Jul 20 '16 at 9:40
  • I will update the answer later. – kiamlaluno Jul 20 '16 at 10:32
  • I suggested a method to find out which variables are used from a theme hook. – kiamlaluno Jul 20 '16 at 17:49
1

Assuming Drupal 7, and assuming $table_rows and $table_headers contain valid data, this is what you need:

$block['content'] = [
  '#theme' => 'table',
  '#rows' => $table_rows,
  '#header' => $table_headers,
  '#empty' => 'Nothing to see here...',
];
0

Assuming drupal 7, and assuming $table_headers and $table_rows have correct data.

$table_headers = array(t('User'), t('Relation'),t('Period') );
foreach ($result as $row) {
            $table_rows[] = array(
            $row->name,
            $row->relation,
            $row->period_from,
           );
         }

$output = theme('table', array('header' => $table_headers, 'rows'=> $table_rows));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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