I wonder how can I pass a form and other variables (an array) to hook_theme(), I know that if you want to render a form in hook theme you have to use 'render element' so now I can't use 'variables' to pass my variable array. here is what I did to render the form:

function example_theme() {
  return array(
    'deductible_form' => array(
      'arguments'      => array('form' => NULL),
      'template'       => 'example',    
      'render element' => 'form',    

What can I do to solve this?


As you rightly say you can't pass variables to a theme function that uses a render element, but then you don't need to.

One of the points of the render element is that the element already contains all of the properties/variables that it needs to perform the render, without having to get them from anywhere else.

A themed form is actually a good example for this: if you need to add extra variables in order to render that form, then they should be arguments to the form function itself. e.g.:

function deductible_form($form, &$form_state, $var1, $var2) {
  $form['#theme'] = 'deductible_form';
  $form['#var1'] = $var1;
  $form['#var2'] = $var2;

  // etc...

  return $form;

Then in your form's theme function you can easily access those vars:

function theme_deductible_form($vars) {
  $form = $vars['form'];

  $var1 = $form['#var1'];
  $var2 = $form['#var2'];

  // Render the form

All you need to do to get that form to render properly now is to call drupal_get_form() with your extra variables:

$form = drupal_get_form('deductible_form', $var1, $var2);
  • thanks you for this info, but i was talking about how to pass the variable to the template itself so i can access in template.tpl.php itself – Ramy Selim May 29 '12 at 11:30
  • 2
    It's exactly the same method...in your template file your vars will be in $form['#var1'], $form['#var2'] etc – Clive May 29 '12 at 11:33

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.