[英]Drupal 7 Edit User Page Override
I need to override the 'user/%user/edit' page for a specific user role, say 'foo_role'. 我需要覆盖特定用户角色的“ user /%user / edit”页面,例如“ 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.
我已经成功创建了新的用户注册表单,该表单明确选择了用于创建新foo_role用户的字段,因为存在其他不适用于foo_role的用户字段,因此此角色的编辑页面不正确。
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;
}
Alter hooks use variable reference to modify variable contents in memory. Alter挂钩使用变量引用来修改内存中的变量内容。 Because of that, you also don't need to return a value in alter hooks.
因此,您也不需要在alter hooks中返回值。
Should look like this: 应该是这样的:
function registermodule_form_user_profile_form_alter(&$form, &$form_state){
global $user;
if(in_array("foo_role", $user->roles)){
$form['field_non_foo'] = FALSE;
}
}
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');
尝试添加drupal_set_message('用户为foo角色'); inside the if condition.
在if条件内。
If that shows, then it is a problem with the unset. 如果显示,则表示未设置有问题。 Note that you shouldn't use unset but instead set '#access' to FALSE.
请注意,您不应使用unset,而应将'#access'设置为FALSE。 Like this:
像这样:
$form['field_non_foo']['#access'] = FALSE;
You could even go fancy and directly save whatever is returned from in_array_check(): 您甚至可以看中并直接保存in_array_check()返回的内容:
$form['field_non_foo']['#access'] = in_array('foo_role', $user->roles);
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. 但是,这里有一个区别,就是#access现在将被强制为TRUE或FALSE,并且不使用它可能已经拥有的任何值。
Edit: Are you sure that your field isn't inside a fieldset? 编辑:您确定您的字段不在字段集中吗? Then it would be $form['the_fieldset']['field_non_foo'] instead.
然后它将是$ form ['the_fieldset'] ['field_non_foo']。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.