Drupal 规则 php 操作

标签 drupal redirect drupal-rules

我第一次尝试规则模块,并尝试使用一些简单的 php 代码重定向我的用户,如下所示:

drupal_set_message('testing');
drupal_goto('node/3');

第一行代码执行,但第二行代码应该将我的用户引导到node/3,但没有达到预期的效果。

如何才能使用此重定向功能?

最佳答案

这很可能是因为页面 URL 中有 ?destination=some/path,这些行位于 drupal_goto() 中导致传递给函数的任何路径都会被 URL 中的内容覆盖:

if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
  $destination = drupal_parse_url($_GET['destination']);
  $path = $destination['path'];
  // ...

您只需将代码更改为以下即可解决此问题:

if (isset($_GET['destination'])) {
  unset($_GET['destination']);
}
drupal_goto('node/3');

如果这不起作用,请尝试在 drupal_goto 之前添加此行:

drupal_static_reset('drupal_get_destination');

这将重置 drupal_get_destination() 函数的静态缓存,该函数在某些时候也会参与此过程(我认为)。

如果一切都失败了,那就走老路:

$path = 'node/3';
$options = array('absolute' => TRUE);
$url = url($path, $options);
$http_response_code = 302;
header('Location: ' . $url, TRUE, $http_response_code);
drupal_exit($url);

这几乎直接来自 drupal_goto() 函数本身,并且肯定会起作用。

关于Drupal 规则 php 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8480687/

相关文章:

drupal - 如何在 Drupal 中创建和管理页脚菜单?

html - 在 htaccess 中删除 html 扩展后永久重定向

drupal - 使用 token 作为数据选择器

Drupal 允许查看未发布的内容

php - 允许用户/管理员自行更新内容管理

c# - ActionFilter - 发送 HTTP header 后无法重定向

drupal-7 - 使用 Drupal 规则向具有角色的用户发送电子邮件,但仅发送一次

drupal-7 - 如何在 Drupal 7 中使用规则创建新节点

php - drupal 在哪里保存我从其表中的内容类型上传的图像路径?

重定向 GitHub Pages 上的多个网页