0

I'm implementing hook_theme() in a custom module.

function monitor_chart_theme() {
  return array(
    'chart_monitor' => array(
      'template' => 'monitor_chart',
      'variables' => array(
        'container' => null,
        'theme' => null,
        'classes' => null,
        'chart' => null,
      ),
    ),
  );
}

On the template file, I would like to add additional CSS classes the theme function gets in its parameters. I declared a preprocess function with the following code.

function monitor_chart_preprocess_chart_monitor(&$vars) {
  if ($vars['classes']) {
    foreach ($vars['classes'] as $class) {
      $vars['classes_array'][] = $class;
    }
  }
}

The template file contains the following code.

<div id="<?php print $container; ?>" class="<?php print $classes; ?>"></div>

If I don't use the preprocess function, the class attribute of <div> contains just 'chart-monitor', the name of the theme function.

Is there a more elegant way to pass extra variables to a template file?

0

Yes, arguments can be passed when calling the theme function:

$tpl_data = array(
    'data1' => $data1,
    'data2' => $data2,
);

return theme('my_theme', $tpl_data);

Then in the template $data1 and $data2 will be available.

To add a variable passed here to the default drupal class variable in the template we could add the line:

$class .= " $data1";

before the $class variable is printed.

However I think it's questionable whether or not this method is preferable to your original.

5
  • This is ok, but if on my tpl I cant print $data1 in "class" attribute, because "class" attribute as the default value, the name of my function (I've updated the post)
    – arrubiu
    Feb 26 '14 at 15:44
  • Sorry, I don't think I quite understand what your trying to achieve.
    – Felix Eve
    Feb 26 '14 at 15:48
  • I want to add a custom class to default one used Drupal, which is chart-monitor. The preprocess method works, but I don't know if there is a better way.
    – arrubiu
    Feb 26 '14 at 15:50
  • I've updated my answer but I not convinced it's the best way to do it. Sorry I couldn't help more...
    – Felix Eve
    Feb 26 '14 at 15:59
  • I've already try it, it doesn't work. The attribute "class" of my tpl is simply ignored :( Thanks
    – arrubiu
    Feb 26 '14 at 16:01
0

The only way to pass values to a template file used for a theme function is adding them to the array passed as second argument of theme(). In the case of that chart_monitor theme function, theme('chart_monitor', array('classes' => array('new_chart_class'))); would add new_chart_class to the CSS classes.

monitor_chart_preprocess_chart_monitor() doesn't need a foreach() loop.

function monitor_chart_preprocess_chart_monitor(&$vars) {
  if (is_array($vars['classes']) && !empty($vars['classes'])) {
    $vars['classes_array'] = array_merge($vars['classes_array'], $vars['classes']);
  }
}

As side note, chart_monitor is the CSS class added by template_preprocess() which is called for every theme function for which preprocess functions aren't disabled.

// Initialize html class attribute for the current hook.
$variables['classes_array'] = array(
  drupal_html_class($hook),
);

template_process() then initializes the $classes variable a template file gets.

// Flatten out classes.
$variables['classes'] = implode(' ', $variables['classes_array']);

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.