0

I try to rewrite

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

into:

$build = array();   

$build['table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows);

 $build['variables'] = array(
'variables' =>$my_vars);

 return $build;

But it does't work:

$build['variables'] = array('variables' =>$my_vars);

How can I do this?

1 Answer 1

1

Since the second argument of theme() is an array, you are writing code for Drupal 7. In that case, theme('table') doesn't use any array item whose index is an integer. The only array indexes you should pass as second argument to theme('table') are the following:

  • header
  • rows
  • attributes
  • caption
  • colgroups
  • stricky
  • empty

Since you are using array('variables' =>$my_vars) it could be you are trying to set attributes, but in that case the array should be an array of HTML attributes, or colgroups, and in that case the array should be similar to the following one.

$colgroup = array(
  // COLGROUP with one COL element.
  array(
    array(
      'class' => array('funky'), // Attribute for the COL element.
    ),
  ),
  // COLGROUP with attributes and inner COL elements.
  array(
    'data' => array(
      array(
        'class' => array('funky'), // Attribute for the COL element.
      ),
    ),
    'class' => array('jazzy'), // Attribute for the COLGROUP element.
  ),
);

If you are trying to set attributes, your code should set the $build variable as following.

$build['table'] = array(
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
  '#attributes' => $my_vars,
);

Replace '#attributes' with '#colgroups' to set colgroups.

In the case theme('table') accepted variables (which could be the case if a module overrodr the default implementation of the theme function), then the code should be similar to the following one.

$build['table'] = array(
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
  '#variables' => $my_vars,
);
4
  • What I am trying to do, is to set some variables. that can be used in my template. My steps are following: 1) function mymodule_theme() { return array( 'my_file' => array( 'template' => 'my_page', ), ); }; 2) /*In my output function*/ $build['variables'] = array( '#theme' => 'datei_includieren', '#variables' => $my_vars, ); return $build; 3) /* my my_page.tpl.php file / <?php echo $my_vars[0]; ?> / the error says, $my_vars is unknown */ Commented Dec 11, 2015 at 10:55
  • You need to show the exact code you are using, in your question, including your output function. Actually, the question you asked is different from the question you should have asked: Your current question is about using a specific Drupal theme function, but your true question would be about a theme function you implemented. The answers for those questions are quite different. Commented Dec 11, 2015 at 11:08
  • I would suggest you to ask a new question about your code. The question you asked here has been answered, at the best it could be answered. Commented Dec 11, 2015 at 11:09
  • yes, you are right. I will open a new question. Thank you for help. I was not exactly, what I was looking for, but very helpfull. Commented Dec 11, 2015 at 12:29

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.