Drupal hook_form_alter匿名用户看不到字段

发布于 2024-10-06 19:18:19 字数 1725 浏览 6 评论 0原文

我创建了一个用于更改表单的小模块,名为“form_mods”。我要更改的表单是“user_profile_form”。我添加了一个名为“个人资料”的额外字段类别。 我在 Drupal 管理中创建了一个名为“profile_state”的选择字段,并在我的模块中对其进行了更改,以拥有一个键 =>州的值列表,当我以管理员身份登录时,它对我有用,但尝试注册的匿名用户会看到一个空的州选择字段。这里有权限问题吗?我尝试添加“访问”=> user_access('访问内容') 到该字段,但这不起作用。这是我的代码:

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;

}

谢谢

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'); // 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦在夏天 2024-10-13 19:18:19

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

OR

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

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

面如桃花 2024-10-13 19:18:19

谢谢 mikesir87 提供的参考链接。我找到了我的问题。
有两种不同的表单正在使用我创建的这些字段。他们有不同的表单 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'] : ''
            );

    }

}

谢谢

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

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