简体   繁体   English

Drupal - 在hook_form_alter中设置默认值?

[英]Drupal - Set default value in hook_form_alter?

Trying to prepopulate some of my form fields, and am using hook_form_alter(). 试图预先填充我的一些表单字段,并使用hook_form_alter()。 I've tried a couple of different ways, but in both cases, the fields still come up empty. 我尝试了几种不同的方法,但在这两种情况下,字段仍然是空的。 I'm assuming that I need to set default_value and not value because if the user changes what's in the field, I want that to update correctly. 我假设我需要设置default_value而不是值,因为如果用户更改了字段中的内容,我希望它能够正确更新。 Is that right? 是对的吗?

Here's what I've been trying: 这是我一直在尝试的:

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

    if($form_id == 'user_profile_form') {

        if(arg(0) == 'user' && arg(1)) {

            $user = user_load(arg(1));

            $form['profile_company_site']= array('#default_value' => $user->profile_company_site);
            $form['profile_blog_url']= array('#default_value' => $user->profile_blog_url);
            $form['profile_my_website_url']= array('#default_value' => $user->profile_my_website_url);
            $form['profile_first_name']= array('#default_value' => $user->profile_first_name);
            $form['profile_last_name']= array('#default_value' => $user->profile_last_name);

        }
    }

}

I also tried it this way: 我也这样试过:

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

    if($form_id == 'user_profile_form') {

        if(arg(0) == 'user' && arg(1)) {

            $user = user_load(arg(1));

            $form['profile_company_site'][#default_value'] = $user->profile_company_site);
            $form['profile_blog_url'][#default_value'] = $user->profile_blog_url);
            $form['profile_my_website_url']['#default_value'] = $user->profile_my_website_url);
            $form['profile_first_name']['#default_value'] = $user->profile_first_name);
            $form['profile_last_name']['#default_value'] = $user->profile_last_name);

        }
    }

}

Both seem to be in the correct format, but your first one will overwrite all the other items you set for the field. 两者似乎都是正确的格式,但您的第一个将覆盖您为该字段设置的所有其他项目。 So you are better going off with the second and adding them piece meal. 所以你最好和第二个人一起吃点东西。

On the second one, you are missing some single quotes on a couple. 在第二个,你错过了一对夫妇的一些单引号。

$form['profile_company_site']['#default_value'] = $user->profile_company_site);

Are you sure you are getting into loop? 你确定你进入了循环吗?

You are missing one array level. 您缺少一个阵列级别。 The profile form fields will not be at the top level in the $form array, but in a subarray keyed by the category name. 配置文件表单字段不在$ form数组的顶层,而是在由类别名称键入的子数组中。 So if you assigned your fields a category of 'example category', your code should look like this: 因此,如果您为字段分配了“示例类别”类别,则代码应如下所示:

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

    if($form_id == 'user_profile_form') {

        if(arg(0) == 'user' && arg(1)) {

            $user = user_load(arg(1));

            $form['example category']['profile_company_site']['#default_value'] = $user->profile_company_site);
            $form['example category']['profile_blog_url']['#default_value'] = $user->profile_blog_url);
            $form['example category']['profile_my_website_url']['#default_value'] = $user->profile_my_website_url);
            $form['example category']['profile_first_name']['#default_value'] = $user->profile_first_name);
            $form['example category']['profile_last_name']['#default_value'] = $user->profile_last_name);
        }
    }
}

You should use a debugger (or at least a var_dump() ) to inspect the form array you want to manipulate - saves a lot of time. 您应该使用调试器(或至少一个var_dump() )来检查您想要操作的表单数组 - 节省大量时间。

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

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