Drupal 7 编辑用户页面覆盖

发布于 2024-10-18 02:50:32 字数 421 浏览 6 评论 0原文

我需要覆盖特定用户角色的“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑轻吟梦一曲 2024-10-25 02:50:32

Alter hooks 使用变量引用来修改内存中的变量内容。因此,您也不需要在 alter hooks 中返回值。

应该看起来像这样:

function registermodule_form_user_profile_form_alter(&$form, &$form_state){
  global $user;

  if(in_array("foo_role", $user->roles)){
    $form['field_non_foo'] = FALSE;
  }   
}

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:

function registermodule_form_user_profile_form_alter(&$form, &$form_state){
  global $user;

  if(in_array("foo_role", $user->roles)){
    $form['field_non_foo'] = FALSE;
  }   
}
迷爱 2024-10-25 02:50:32

你能澄清一下“失败”吗?

首先,你错过了 &来自 $form.首先改变这一点。

如果这不能解决问题,请尝试找出有多少代码实际上在工作。尝试添加 drupal_set_message('user is in foo role');在 if 条件内。

如果出现这种情况,则说明未设置有问题。请注意,您不应使用 unset,而应将 '#access' 设置为 FALSE。像这样:

$form['field_non_foo']['#access'] = FALSE;

您甚至可以花哨并直接保存从 in_array_check() 返回的任何内容:

$form['field_non_foo']['#access'] = in_array('foo_role', $user->roles);

但是这里有一个区别,那就是 #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:

$form['field_non_foo']['#access'] = FALSE;

You could even go fancy and directly save whatever is returned from 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.

Edit: Are you sure that your field isn't inside a fieldset? Then it would be $form['the_fieldset']['field_non_foo'] instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文