我有一个Drupal网站,我想根据用户输入的个人资料字段显示不同的欢迎页面。我不能使用全局变量$ user,因为用户没有自动登录(他们必须验证电子邮件地址才能登录)。
在哪里可以添加代码以设置重定向?我尝试了在表单验证器中使用$form['#redirect']和$form_state['redirect'],但没有成功。
#implementation of hook_user
mymodule_user($op) {
if ($op == 'login') {
$_REQUEST['destination'] = '/user/will/be/redirected/here'
}
}
重要的部分是确保在user.module中最终调用drupal_goto()
时,您已经设置了$_REQUEST['destination']
。drupal_goto()
。特别是不要在hook_user或hook_form_alter中发出。drupal_goto将禁止调用其他钩子; 至少会破坏功能,但通常会破坏数据库。
4. 不要在"_submit"等form_alter回调中发出drupal_goto()
,这可能会破坏许多其他模块,甚至可能会破坏您的数据库。与Berke的答案类似,但似乎您只希望这是一次性的。对此,您可以检查$account->access属性以检查其上次登录时间。如果为0,则表示他们第一次登录。
无论是电子邮件验证还是非电子邮件验证,这都应该可以正常工作。
<?php
/**
* Implements hook_user().
*/
function mymodule_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'login':
// execute this if they have never accessed the site before
if ($account->access == 0) {
// run conditional logic based on profile fields
// to set destination here
$_REQUEST['destination'] = 'path/to/welcome-page';
}
break;
}
}
?>
if(($op == 'login') && ($account->access == 0)) {
替换它。这将避免很多人大脑炸裂。 - berkes