views:

878

answers:

2

Trying to prepopulate some of my form fields, and am using 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. 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);

     }
    }

}
+2  A: 

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?

easement
+1 - except for the missing array level, this is correct advice
Henrik Opel
+1  A: 

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

Henrik Opel
FYI the devel module provides a 'pretty' version of var_dump(), in the function dsm().
threecheeseopera
Thanks, that did it!
n00b0101