简体   繁体   English

如何正确地将主题区域添加到 views-view--page.tpl.php in Drupal 7

[英]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!好的,这就是我所拥有和发现的(有效 - 但它会产生日志通知)“只有变量应该在 eval() 中通过引用传递”这让我非常疯狂!

Initially like any theme region I added the regions I wanted for views use to the theme.info file.最初像任何主题区域一样,我将我想要用于视图的区域添加到 theme.info 文件中。

; 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)在 views-view--page.tpl.php 中,我放置了这些(我希望区域呈现的位置)

<?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%这会为 tpl 中的每个条目生成一个通知,但它按预期 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; ?>然后我尝试在主题模板中添加一个钩子。php(试图从中创建变量)所以我可以使用<?php print $view_header; ?> <?php print $view_header; ?> like so: <?php print $view_header; ?>像这样:

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).这再次有效 - 这再次产生日志通知.. 这次所有页面加载的每个区域 3 个,所以 9 个而不是 3 个(不仅仅是当 views-view--page.tpl.php 加载时)追溯到主题template.php(这次与上面列出的通知相同,但在加载该页面时,视图模板中使用的 $variables 没有通知)。

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?如何使主题区域在 tpl 中可用变量?

  • I'm under the impression everything I've found is years old (3 to 10) and likely worked fine for php 5.x - this site is currently using php 7.2.x (should that make a difference in how this needs to be done)我的印象是我发现的所有东西都是几年前的(3 到 10 年)并且可能在 php 5.x 上运行良好 - 这个网站目前正在使用 php 7.2.x(这应该会改变它的需要完毕)

Any help here would be greatly appreciated, thank you!这里的任何帮助将不胜感激,谢谢!

This is because the function render() expects a reference, and only variables should be passed by reference :这是因为 function render()需要一个引用,并且只有变量应该通过引用传递

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:另外,我认为通往 go 的方法应该是将可渲染的 arrays 分配给预处理挂钩中的$variables

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() : ...并让模板调用render()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM