3

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?

1 Answer 1

5

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 unformatted output, but I do it somewhat often at the field level.

1
  • Took me a looooooong time to figure this out. Is there a part of the documentation somewhere that explains hierarchy of how views are "built up from the inside out"?
    – YPCrumble
    Commented Mar 15, 2016 at 18:47

Your Answer

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

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