【问题标题】:Drupal hook_form_alter anonymous user can't see the fieldsDrupal hook_form_alter 匿名用户看不到字段
【发布时间】:2026-02-12 12:10:02
【问题描述】:

我创建了一个名为“form_mods”的用于更改表单的小模块。我正在更改的表单是“user_profile_form”。我为名为“个人资料”的额外字段添加了一个类别。 我在 Drupal 管理员中创建了一个名为“profile_state”的选择字段,并在我的模块中将其更改为具有状态的键 => 值列表,并且当以管理员身份登录但匿名用户试图注册看到一个空的状态选择字段。这里有权限问题吗?我尝试将 'access' => user_access('access content') 添加到该字段,但这没有用。这是我的代码:

function form_mods_form_alter($form, $form_state, $form_id) {

    switch ($form_id) {

        ##  user_profile_form  ###################################################################################
        case 'user_profile_form': // This is our form ID.

            //echo '###'.$form['_category']['#value'].'###';
            if($form['_category']['#value'] == 'Profile'){  

                // Modify the states dropdown list
                $states = load_states_list();   
                $form['Profile']['profile_state'] = array(
                  '#type' => 'select',
                  '#title' => t('State'),
                  '#options' => $states,
                  '#required' => TRUE,
                  '#default_value' => isset($form['Profile']['profile_state']['#default_value']) ? $form['Profile']['profile_state']['#default_value'] : '',
                  '#element_validate' => array('profile_state_validate')
                );

            }
        ####################################################################################
        break;

    }   

}

function load_states_list() {

    $states = array('' => '-- Select a state'); // add a default empty value
    $results = db_query("SELECT * FROM {states_list} ORDER BY name ASC");
    while ($state = db_fetch_array($results)) {
      $states[$state['code']] = $state['name'];
    }

    return $states;

}

谢谢

【问题讨论】:

  • 不是挖掘一个老问题,但如果这个问题解决了,你能把我的标记为答案吗?到目前为止,它已经获得了好几票,并解决了手头的问题。我正在尝试清理我松散的答案。谢谢!

标签: drupal forms permissions hook alter


【解决方案1】:

首先,你确定你的函数曾经运行过吗?以您的方式命名函数,我认为不是。如果模块名称是“form_mods”,你的函数应该被调用

function form_mods_form_alter

或者...由于您只是修改 user_profile_form 表单,您可以使用函数名称

function form_mods_user_profile_form_alter

现在,它不起作用的原因是因为您在参数列表中的 $form 前面没有 &。 & 基本上将变量作为引用传递,因此您对 $form 变量所做的任何更改都将被保存并传递回调用函数。所以,你的函数应该是这样的

function form_mods_form_alter(&$form, &$form_state, $form_id) {

function form_mods_user_profile_form_alter(&$form, &$form_state) {

参考:http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_form_alter/6

【讨论】:

    【解决方案2】:

    感谢 mikesir87 提供参考链接。我发现了我的问题。 有 2 种不同的表单正在使用我创建的这些字段。他们有不同的表格ID。我必须查找“user_profile_form”和“user_register”表单 ID。

    这是我的新代码:

    function form_mods_form_alter($form, $form_state, $form_id) {
    
        if( (($form_id == 'user_profile_form') && ($form['_category']['#value'] == 'Profile')) || ($form_id == 'user_register') ){
    
                // Modify the states dropdown list
                $states = form_mods_load_states_list(); 
                $form['Profile']['profile_state'] = array(
                  '#type' => 'select',
                  '#title' => t('State'),
                  '#options' => $states,
                  '#required' => TRUE,
                  '#default_value' => isset($form['Profile']['profile_state']['#default_value']) ? $form['Profile']['profile_state']['#default_value'] : ''
                );
    
        }
    
    }
    

    谢谢

    【讨论】:

    • 请更新您的原始帖子。此外,您似乎没有正确重命名函数。如果该函数位于您创建的“form_mods”模块中,则该函数应称为 form_mods_form_alter()
    • 谢谢埃里克。现在已经更正了。你的意思是我用我的解决方案更新我的原始帖子而不是回答它?谢谢