sidgrafix
sidgrafix

Reputation: 132

How do I properly add a theme region to a views-view--page.tpl.php in Drupal 7

OK, so here is what I have and found (which works - however it produces a log Notice) "Only variables should be passed by reference in eval()" which is driving me absolutely crazy!

Initially like any theme region I added the regions I wanted for views use to the theme.info file.

; View Regions
regions[view_header]    = View Header
regions[view_content]   = View Content
regions[view_footer]    = View Footer

In the views-view--page.tpl.php I placed these (where I wanted the region to render)

<?php print render(block_get_blocks_by_region('view_header')); ?>
<?php print render(block_get_blocks_by_region('view_content')); ?>
<?php print render(block_get_blocks_by_region('view_footer')); ?>

This produces a notice for each entry in the tpl yet it works as expected 100%

I then tried adding a hook in the themes template.php (in an attempt to make variables out of them) so I can just use <?php print $view_header; ?> like so:

function theme_preprocess_views_view(&$variables){
  $variables['view_header'] = render(block_get_blocks_by_region('view_header'));
  $variables['view_content'] = render(block_get_blocks_by_region('view_content'));
  $variables['view_footer'] = render(block_get_blocks_by_region('view_footer'));

Again this works - and again this produces log notices.. this time 3 for each region on all page loads, so 9 instead of 3 (not just when the views-view--page.tpl.php loads) which traced back to theme template.php (same notice as listed above this time but no notice from the $variables used in the views template when that page loads).

Obviously I'm missing something here! Maybe this is entirely the wrong way to do this...

How do I make the theme regions usable variables in tpl's?

Any help here would be greatly appreciated, thank you!

Upvotes: 1

Views: 161

Answers (1)

EricLavault
EricLavault

Reputation: 16095

This is because the function render() expects a reference, and only variables should be passed by reference :

function render(&$element) {
  # code need to be able to modify $element
}

render('value');                                   # throws notice
render(block_get_blocks_by_region('view_header')); # throws notice

$value = 'value';
$build = block_get_blocks_by_region('view_header');
render($value); # Ok
render($build); # Ok

Also, I think the way to go should be to assign the renderable arrays to $variables in the preprocess hook :

function theme_preprocess_views_view(&$variables){
  $variables['view_header'] = block_get_blocks_by_region('view_header');
  $variables['view_content'] = block_get_blocks_by_region('view_content');
  $variables['view_footer'] = block_get_blocks_by_region('view_footer');
  # ...
}

... and let the template call render() :

<?php print render($view_header); ?>
<?php print render($view_content); ?>
<?php print render($view_footer); ?>

Upvotes: 1

Related Questions