Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

The variables I'm adding in my preprocessor function are not visible in the template for the unformatted view I've written. This seems to work fine if the template is formatted (the default).

That is, in template.php I set some variable $foo in the preprocessor…

function themename_preprocess_views_view(&$vars) {
  // Do view-specific preprocessing in here because of http://drupal.org/node/939462
  if ( $vars['view']->name === "viewname" ) {
    $vars['foo'] = 'bar'; // set $foo to be used in viewname templates
  }
}

…such that I can use it in views-view--viewname.tpl.php, <?php echo $foo; ?>. This works just fine.

However, $foo is undefined in views-view-unformatted--viewname.tpl.php.

Why is this? How can I store the results of my preprocessing in a variable I can then use in the template for my unformatted view?

Doing the preprocessing in a function specific to the unformatted view doesn't work because

  • such a function, themename_preprocess_views_view_unformatted__viewname(), will not be called due to a bug, and

  • working around the bug by defining its behaviour in, or calling it from, themename_preprocess_views_view() doesn't work

    • as above, where I seem to make changes to $vars that should be accessible in all viewname view templates,
    • nor can it be repeated specifically for the unformatted view templates because there's nothing in $vars that can be used to discriminate the unformatted from the default view.

Any help?

share|improve this question

1 Answer

up vote 1 down vote accepted

Your problem is that the output for the view is built up from the inside-out. So, the views-view template already has the processed results from the views-view-unformatted template.

You should just be able to implement a template_preprocess_views_view_unformatted(), and do a manual check:

function mytheme_preprocess_views_view_unformatted(&$variables) {
  $view = $variables['view'];

  if ($view->name == 'viewname') {
    $variables['foo'] = 'bar';
  }
}

I don't typically use the with the unformatted output, but I do it somewhat often at the field level.

share|improve this answer
Aha! Yes, that makes sense. Thanks. – lucasrizoli Mar 14 at 19:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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