Is there a way of selecting a Default Address per Address Field.

Eg:
Country: United States
Street Address: (Empty)
City: Brooklyn
State: New York
Zip Code: 98590

An Alert Content Type that i would be creating would use the same City, State, Country, and ZipCode......the only thing that would be different the majority of times would be the Street Address. Can i just create a default selection and allow the user to optionally change it if needed? That would really improve the UX for my use-case and possibly others(Mapping, News etc....)

Comments

gcharles created an issue. See original summary.

dww’s picture

Version: 8.x-1.0-beta3 » 8.x-1.x-dev

There's partially broken code that's trying to hide the default value from the field settings. The intention was to only let a field administrator select a default country (via a setting on the field widget), but not a default field value in the usual way. However, in practice, the default value is still getting set in some cases, but you can't change it (leading to a bug where once you define a default country via the field widget, you can never change it). See these two issues for more:

#2838457: Re-enable the default value functionality for Address fields
#2760387: Default country is imposed on fields with an empty address (which currently prevents changing the default country at all)

I marked this issue as related to #2838457 since I'm not yet entirely sure why we don't allow a default address. We should have a bigger-picture solution to those bugs that takes into account the desire to set a default address like this.

I'm trying to get in touch with the address module maintainer to see how he wants to solve all these issues for real, then I'll be working on patches. Stay tuned.

Cheers,
-Derek

dww’s picture

Status: Active » Closed (won't fix)

Had a brief chat in IRC with bojanz about this. He pointed me to the addressfield issue queue where this stuff has been discussed at great length. In particular:

#2392863: Remove the default values instance setting, introduce a default country instance setting
#2392059: Choosing -None- still saves a row in the database
#2392855: Provide a hook for altering address defaults
...

So yeah, address.module is not going to support the ability to define a default address like other fields do. There is a default country option in the field widget settings, and there's an event you can subscribe to which lets you alter the default address:

    // Allow other modules to alter the values.
    $event = new InitialValuesEvent($initial_values, $this->fieldDefinition);
    $this->eventDispatcher->dispatch(AddressEvents::INITIAL_VALUES, $event);
    $initial_values = $event->getInitialValues();

We should probably add some documentation on how to use that event subscription stuff, perhaps in an address.api.php file. But that's the way this has to work. There are too many problems with the way core handles default values for multi-column fields for address.module to do the normal thing.

Sorry,
-Derek

jaykandari’s picture

Update:
This event as mentioned in #3 is no longer fired. Use hook_field_widget_form_alter() instead.
As per: AddressEvents::INITIAL_VALUES

See also: https://docs.drupalcommerce.org/commerce2/developer-guide/customers/addr...

Example:

/**
 * Implements hook_field_widget_form_alter().
 */
function mymodule_field_widget_form_alter(&$element, &$form_state, $context) {
  // Changing the default address thru field settings is not possible
  // Thus changing it via code.
  if ($context['widget']->getPluginId() == 'address_default') {
    $element['address']['#default_value']['country_code'] = 'GB';
  }
}

liquidcms’s picture

deleted.