开发者

Drupal hook_form_alter anonymous user can't see the fields

I created a small module for altering forms called "form_mods". The form I'm altering is the "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. Is there a permissions issue here? I tried to add 'access' => user_access('access content') to the field but that didn't work. 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开发者_如何学JAVA'); // 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

function form_mods_form_alter

Or...since you're only modifying the user_profile_form form, you could use the function name

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. 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. 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


Thank you mikesir87 for the reference link. I figured out my problem. There are 2 different forms that are using those fields I created. They have different form id's. I have to look for "user_profile_form" and "user_register" form id's.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜