Question

I am porting a module from Drupal 6 to Drupal 7 and I am trying to pass a variable from my custom module to a template. I have something like this:

function my_callback_function(){

  ... //some unrelated code

  $page_params = array();
  $page_params['items_per_page'] = 25;
  $page_params['page'] = $_GET['page'] ? $_GET['page'] : 0;
  $page_params['total_items'] = $data_provider->getNumItems();
  $page_params['total_pages'] = $data_provider->getNumPages($page_params['items_per_page']);

  return theme('my_theme', $page_params);
}


function my_module_theme($existing, $type, $theme, $path) {
  return array(
    'my_theme' => array(
      'variables' => array('page_params' => NULL),
      'template' => 'theme/my_template_file',
    ),
  );
}

And inside *my_template_file.tpl.php* I try to use $page_params:

<?php print $page_params['total_items']; ?>

All that make my site to throw the following error:

Fatal error: Unsupported operand types in C:...\includes\theme.inc on line 1075

Which corresponds with these lines of code in theme.inc:

// Merge in argument defaults.
  if (!empty($info['variables'])) {
    $variables += $info['variables']; // THIS IS THE VERY EXACT LINE
  }
  elseif (!empty($info['render element'])) {
    $variables += array($info['render element'] => array());
  }

If I leave the theme() call as it was in Drupal 6, the error doesn't appear but then my template doesn't recognize my $page_params variable:

  return theme('my_theme', array('page_params' => $page_params));

I have read half the API trying to figure out what I am doing wrong but as far as I have read, it seems that this is the proper way to pass variables from a custom module to a template. Thus, any kind of help will be more than welcome.

Thanks in advance.

Was it helpful?

Solution

Finally, I figured out what I was doing wrong. In fact, they were a couple of things:

My theme() call was ok:

return theme('my_theme', $page_params);

But my hook_theme implementation wasn't. If $page_params is my array of variables, i cannot use the whole array as a variable, I have to explicitly specify which are my variables inside the array. Something like this:

function my_module_theme($existing, $type, $theme, $path) {
  return array(
    'my_theme' => array(
          'variables' => array(
            'items_per_page' => NULL,
            'page' => NULL,
            'total_items' => NULL,
            'total_pages' => NULL,
          ),
    'template' => 'theme/my_template_file',
  );
}

And finally, inside my_template_file.tpl.php I will have to use the variable names directly instead of using them as a component of $page_params:

<?php print $total_items; ?>

It may seem obvious for experienced users but it took me a while until I realized this. I hope it can be useful for other beginners like me.

OTHER TIPS

You can use drupal variable_set() and variable_get() to store data in drupal session and get data from session.

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top