简体   繁体   English

Drupal hook_form_alter匿名用户无法看到字段

[英]Drupal hook_form_alter anonymous user can't see the fields

I created a small module for altering forms called "form_mods". 我创建了一个用于更改表单的小模块,称为“ form_mods”。 The form I'm altering is the "user_profile_form". 我要更改的表单是“ user_profile_form”。 I added a category for extra fields called "Profile". 我为额外的字段添加了一个类别,称为“个人资料”。 I created a select field in the Drupal admin called "profile_state" and I'm altering it in my module to have a key => value list of states and It's working for me when logged in as an admin but an anonymous user that's trying to register sees an empty states select field. 我在Drupal管理员中创建了一个名为“ profile_state”的选择字段,并在模块中对其进行了更改,使其具有键=>值状态列表,并且以管理员身份登录但为匿名用户工作时对我来说是有效的寄存器看到一个空状态选择字段。 Is there a permissions issue here? 这里有权限问题吗? I tried to add 'access' => user_access('access content') to the field but that didn't work. 我试图将'access'=> user_access('access content')添加到该字段,但这没有用。 Here is my code: 这是我的代码:

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;

}

Thanks 谢谢

First of all, are you sure you're function ever gets run? 首先,您确定自己的功能曾经运行过吗? Having the function named the way you do, I don't think it is. 以函数的方式命名,我认为并非如此。 If the module name is "form_mods", your function should be called 如果模块名称为“ form_mods”,则应调用您的函数

function form_mods_form_alter

Or...since you're only modifying the user_profile_form form, you could use the function name 或者...由于您只修改了user_profile_form表单,因此可以使用函数名称

function form_mods_user_profile_form_alter

Now, the reason it isn't working is because you don't have the & in front of the $form in the parameter list. 现在,它不起作用的原因是因为您在参数列表的$ form前面没有&。 The & basically passes the variable as reference, and so any changes you make to the $form variable will be saved and passed back to the calling function. &基本上将变量作为引用传递,因此您对$ form变量所做的任何更改都将被保存并传递回调用函数。 So, your function should look like 因此,您的函数应该看起来像

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

OR 要么

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

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

Thank you mikesir87 for the reference link. 谢谢mikesir87提供的参考链接。 I figured out my problem. 我发现了我的问题。 There are 2 different forms that are using those fields I created. 有2种不同的形式正在使用我创建的那些字段。 They have different form id's. 它们具有不同的表单ID。 I have to look for "user_profile_form" and "user_register" form id's. 我必须查找“ user_profile_form”和“ user_register”表单ID。

Here is my new code that works: 这是我的新代码有效:

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'] : ''
            );

    }

}

thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM