I have a custom form and want to use the 'address' field type on it, I'm using the example found in the feature request for the field:

    $form['bill_address'] = array(
      '#type' => 'address',
      '#title' => $this->t('Billing Address'),
      '#default_value' => [
        'given_name' => 'John',
        'family_name' => 'Smith',
        'organization' => 'Google Inc.',
        'address_line1' => '1098 Alta Ave',
        'postal_code' => '94043',
        'locality' => 'Mountain View',
        'administrative_area' => 'CA',
        'country_code' => 'BE',
        'langcode' => 'en',
      ],
      '#available_countries' => ['DE', 'FR', 'US'],
    );

The default shows up fine at first, showing all the fields.
When selecting a new country, all the fields disappear, the "please wait" widget is shown for a second and then disappears again but nothing else happens.
Re-selecing the default-country doesn't make the fields re-appear either.

Tried on Chrome regular and incognito and Firefox, same issue everywhere.
Console tab is not showing anything.

Form with default values:
default

Form after selecting a country:
after

Comments

PascCl created an issue. See original summary.

Pascal-’s picture

Pascal-’s picture

Did some debugging, but can't really figure it out myself.

I logged this in processAddress.php:

$value = $element['#value'];
\Drupal::logger('address')->notice(print_r($value, TRUE));

Took screenshots of all the log results:
https://gyazo.com/collections/invitations/3255814c0157b2d6cc08343945f45a9f

dww’s picture

Well, that's interesting. At #2689089-10: Define a form element type "address" bojanz said:

I have it working on multiple forms, Commerce and non-Commerce. We need to identify what makes that issue different.

I tried to reproduce this by hacking my copy of devel.module to add a bogus address form element to the devel settings form. I'm seeing exactly the buggy behavior described in the original post. Haven't had a chance to actually debug, but wanted to confirm and provide a quick patch that should make it easy to reproduce this if anyone's interested in digging further. I'm assuming you have devel installed. ;)

Note: the code in the OP uses 'BE' for the country code, even though it's obviously a US address. My patch uses the correct country for the default value. However, the bug is present in both cases.

bojanz’s picture

The form element works when embedded inside another form element (commerce_profile_select), but on the Devel form, core just ignores all input (hence no country_code and no elements). I managed to briefly catch a glimpse of address properties appearing in user input one level above where they should be, as if $element['#parents'] isn't respected, but the code looks fine. Tried to disable the groupElements/clearValues code in case the bug was there, but no change.
It's even more visible if no #element_value is provided.

First hour wasted. To be continued.

EDIT: I should also note that the form element works inside the field widget (AddressDefaultWidget), and the tests are passing, yet I see no obvious difference between the two "environments".

bojanz’s picture

Adding $form['#tree'] = TRUE; to the SettingsForm solves it, looks like the problem is that the form element does not always enforce '#tree' => TRUE for itself (in the process method). That causes the form values to show up in the wrong place

bojanz’s picture

Status: Active » Needs review
StatusFileSize
new862 bytes

Try this.

bojanz’s picture

Title: 'address' type field on custom form does not show any address fields on selecting a country » Address form element doesn't work on forms without #tree => TRUE
dww’s picture

Status: Needs review » Reviewed & tested by the community

Nice! Confirmed this solves it on my local site. Code looks clean (duh). RTBC.

Thanks,
-Derek

dww’s picture

bojanz’s picture

Status: Reviewed & tested by the community » Fixed

Committed, thanks!

  • bojanz committed dc6db21 on 8.x-1.x
    Issue #2848747 by bojanz, dww, Jodan: Address form element doesn't work...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.