1

我创建了一个名为“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;

}

谢谢

4

2 回答 2

3

首先,你确定你的函数曾经运行过吗?以您的方式命名函数,我认为不是。如果模块名称为“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

于 2010-12-08T14:26:59.120 回答
-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'] : ''
            );

    }

}

谢谢

于 2010-12-08T15:01:14.140 回答