Drupal 7 编辑用户页面覆盖
我需要覆盖特定用户角色的“user/%user/edit”页面,例如“foo_role”。我已经成功创建了新的用户注册表单,该表单显式选择了用于创建新 foo_role 用户的字段,但是,因为还有其他用户字段不适用于 foo_role,所以该角色的编辑页面不正确。
这是我最好的尝试,但失败了:
function registermodule_form_user_profile_form_alter($form, &$form_state){
global $user;
if(in_array("foo_role", $user->roles)){
unset($form['field_non_foo']);
}
return $form;
}
I need to override the 'user/%user/edit' page for a specific user role, say 'foo_role'. I have successfully created the new user registration form that explicitly selects the fields to be used in creating a new foo_role user however, because there are additional user fields not applicable to foo_role the edit page for this role is not correct.
This is my best attempt, but it fails:
function registermodule_form_user_profile_form_alter($form, &$form_state){
global $user;
if(in_array("foo_role", $user->roles)){
unset($form['field_non_foo']);
}
return $form;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Alter hooks 使用变量引用来修改内存中的变量内容。因此,您也不需要在 alter hooks 中返回值。
应该看起来像这样:
Alter hooks use variable reference to modify variable contents in memory. Because of that, you also don't need to return a value in alter hooks.
Should look like this:
你能澄清一下“失败”吗?
首先,你错过了 &来自 $form.首先改变这一点。
如果这不能解决问题,请尝试找出有多少代码实际上在工作。尝试添加 drupal_set_message('user is in foo role');在 if 条件内。
如果出现这种情况,则说明未设置有问题。请注意,您不应使用 unset,而应将 '#access' 设置为 FALSE。像这样:
您甚至可以花哨并直接保存从 in_array_check() 返回的任何内容:
但是这里有一个区别,那就是 #access 现在将被强制为 TRUE 或 FALSE,并且不使用它可能已经具有的任何值。
编辑:您确定您的字段不在字段集中吗?那么它将是 $form['the_fieldset']['field_non_foo'] 。
Can you clarify "fails"?
First, you are missing the & from $form. Change that for a start.
If that doesn't fix it, try to figure out how much of your code is actually working. Try adding a drupal_set_message('user is in foo role'); inside the if condition.
If that shows, then it is a problem with the unset. Note that you shouldn't use unset but instead set '#access' to FALSE. Like this:
You could even go fancy and directly save whatever is returned from in_array_check():
There is however a difference here and that is that #access will now be forced to be either TRUE or FALSE and not use whatever value it might already have.
Edit: Are you sure that your field isn't inside a fieldset? Then it would be $form['the_fieldset']['field_non_foo'] instead.