4

我遇到了一个问题,即 hook_preprocess_page 对 &$variables 的更改未呈现,即使它是 $theme_registry['page']['preprocess functions'] 下的最后一项。将 $variables 的内容记录到文件中会显示内容已更改,但站点上的内容似乎未更改。刷新了drupal上的所有缓存,刷新了所有浏览器缓存,结果仍然相同。

/**
 * Implementation of hook_preprocess_page().
 */
function grinchlist_preprocess_page(&$variables) {

  if (grinchlist_usercheck($variables['user']['uid'])) {
    $variables['scripts'] = preg_replace('/<script[^>]*christmas_snow.*<\/script>/','',$variables['scripts']);
  }
  file_put_contents('/tmp/vars.txt',print_r($variables,true));
}

/tmp/vars.txt 正确显示变量,但浏览器仍显示正在加载的脚本。

这可能是一个愚蠢的例子,但我在其他情况下遇到了 hook_preprocess_page 这个问题,它真的有助于理解这里发生了什么......

谢谢。

4

3 回答 3

3

报告的代码包含错误。IF 语句应从

if (grinchlist_usercheck($variables['user']['uid'])) {
  // ...
}

if (grinchlist_usercheck($variables['user']->uid)) {
  // ...
}

我在我hook_preprocess_page()一个模块中使用,调用的函数确实改变了变量的内容。

然后,正如 Richard M 报道的那样,该函数应该从drupal_get_js().

于 2010-06-17T03:06:25.087 回答
2

I think you probably (assuming this works in the same way as CSS includes) need to call drupal_get_js at the end of your function, like so: $variables['scripts'] = drupal_get_js();.

于 2009-12-23T21:22:25.543 回答
0

我知道这是一个老问题,但我刚刚发现它,我想我知道答案。

我认为 jquery_update 导致了这个。

jquery_update 实现 hook_theme_registry_alter 改变 $theme_registry 以便 jquery_update_preprocess_page 最后运行。尽管彼得在 $theme_registry 中看到了什么,但这是在他查看之后发生的。

jquery_update 从 drupal_add_js() 获取 $scripts,修改数组,然后重置 $variables['scripts'] 覆盖之前所做的任何更改。

我不确定完美的解决方案是什么。我不认为我们真的应该直接弄乱脚本字符串。我有一个特殊的单页案例,所以我可能会做一些不好的事情,即从 jquery_update_preprocess_page 调用我的代码。Drupal 6 的 jquery_update 现在不太可能更新。这似乎比进入谁来最后的决斗要好。

于 2012-03-19T14:33:33.787 回答