I'm trying to use hook_form_alter() to set #default_values in the commerce checkout form based on some session and product information.
I find that #default_value on the individual sub-elements doesn't work as expected due to the #process parameter indicating _addressfield_process_format_form() will run after any form alter hooks.
_addressfield_process_format_form() implements:
$child['#default_value'] = $address[$key];
So, it appears the way to set #default_value is to instead create an #address parameter as an address array on the parent address element. This seems inconsistent with Drupal's core Form API.
I would argue that _addressfield_process_format_form() should at least check if the individual fields have #default_value set before overwriting with (possibly) blank information.
if (!isset($child['#default_value'])) {
$child['#default_value'] = $address[$key];
}
Alternatively (at minimum), for the sake of consistency, the #address key on the parent element should probably be renamed to #default_value.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | addressfield_hook_form_alter-1285980-5.patch | 457 bytes | BassistJimmyJam |
| #4 | addressfield_hook_form_alter-1285980-4.patch | 508 bytes | sokru |
Comments
Comment #1
rtdean93 commentedDid you ever figure out how to set a default element?
I have a GEO IP module which detects the country. I would like to set the country in the Customer Profile form to the detected country.
Comment #2
s_leu commentedThanks c4rl, very usefull.
Comment #3
alexgreyhead commentedThanks for the tip c4rl - I've spent the last few hours of my life on this - very frustrating!
I used a code snippet like this to change the default value of the country field:
Comment #4
sokru commentedAny reason not to use c4rl's method? Attached is a patch against 7.x-1.0-beta3
Comment #5
BassistJimmyJam commentedI ran into this same problem and the proposed solution worked for me. However, I had to re-roll the patch against the latest version of dev.
Comment #6
rszrama commentedI ran into this same issue this evening, and this patch worked for me. This is simpler than fago's patch, but I'll review his to ensure we're not missing something in this fix before just going with this.
Comment #7
muschpusch commented#1662680: Stop duplicating form state values in $form_state['addressfield'] this is fago's approach if someone wants to compare his options....
Comment #8
jody lynnPatch in 5 fixed my issue which was the one described in #1662680: Stop duplicating form state values in $form_state['addressfield']: I had a multiple field collection containing an addressfield and the addresses were picking up default values from the first one entered.Spoke too soon... I have the issue Fago described in #1662680: Stop duplicating form state values in $form_state['addressfield']: I had a multiple field collection containing an addressfield and the addresses were picking up default values from the first one entered. His patch fixed it but this one did not.
Comment #9
jody lynnComment #10
jody lynnComment #11
dgtlmoon commentedPatch in #5 worked for me, @rszrama did you get a chance to do any further testing?
Comment #12
swentel commentedSame comment as in #1662680: Stop duplicating form state values in $form_state['addressfield'].
This doesn't fix the trick. For instance, add 'Canada' as a country. Then do a form alter to set the country default to Canada (but the instance settings is something different) - the province field won't show up.
I'm raising this to major, there is no decent way to change the default value in code (say based on the current domain) and the form being right. I'll try to look deeper into this, but any more eyes on this would be great, because I've got a major headache already trying to get this right.
Comment #13
bojanz commentedswentel is right, this patch doesn't solve anything. It only allows you to set defaults for fields that are the same for all countries (name, company).
Per-country fields (administrative area, locality, etc) have already been built by the time hook_form_alter() runs, so the change doesn't work.
I've opened a new issue that documents the entire problem, please join me there: #2392855: Provide a hook for altering address defaults .