Just upgraded to 7.x-1.0 and my ability to set the token default values for the address field components is gone. In the past version, when editing an addressfield field an example of what the form would look like would appear as you adjusted component settings, country available, single name, etc. This example form would allow the editor to fill each with tokens to display saved user data. Very useful for commerce checkouts since it would prefill the address for returning logged in customers.

Comments

bojanz’s picture

Category: Bug report » Support request
Status: Active » Fixed

You can now only set the default country.
The other defaults were removed in #2392863: Remove the default values instance setting, introduce a default country instance setting, since they didn't work properly for optional fields, and for people trying to set defaults via code.
You can recreate the same functionality using hook_addressfield_default_values_alter().

cybermache’s picture

Thanks for letting me know there wasn't anything wrong on my development install. It saddens me that this happened since it was so easy just to use address tokens to populate address field components with existing user data. You can't make everyone happy, I know, and making the module work better for most is the best path.

So with this change, my only option to reproduce my previous ability is to write a custom module? Besides the example in the included addressfield.api.php file, is there other documentation on how this new hook works?

bojanz’s picture

Yes, custom module. There's no additional documentation, should be straightforward, post your final code here and we'll extend our example if necessary.

aitala’s picture

I'd be interested in seeing @cybermache 's solution...

Eric

cybermache’s picture

Here is what I came up with. Of course it is specific to my needs as it calls/queries to a specific field/table name. Ideally it would provide a admin UI where one could select the address field and fill the fields with the tokens desired. At first I also had this module fix the country value but took it out because it seemed redundant. I realize $GLOBALS['user'] might not be the best to use here as I believe it is being avoided in D8. Please share any mods if you'd liked to. Hope it helps.

sites/all/modules/custom/field_defaults/field_defaults.info

name = Field defaults 
description = Sets default address components in an entityform to token values of active user.
core = 7.x
package = Custom

dependencies[] = entityform
dependencies[] = addressfield
dependencies[] = addressfield_phone

sites/all/modules/custom/field_defaults/field_defaults.module

/**
 * Implements hook_addressfield_default_values_alter().
 */
function field_defaults_addressfield_default_values_alter(&$default_values, $context) {
  //Get the user viewing the entityform
  $user = $GLOBALS['user'];
  $uid = $user->uid;

  if ($uid > '1'): //Don't run for anonymous or user 1
  // Create an array of address field components using uid of current user 
    $sql = "SELECT * FROM {field_data_field_address} WHERE entity_id = :id";
    $result = db_query($sql, array(':id' => $uid));

    if ($result):
      while ($row = $result->fetchAssoc()) {
      	// Build the default address values from query results
      	$address = $row['field_address_thoroughfare'];
      	$address2 = $row['field_address_premise'];
      	$city = $row['field_address_locality'];
      	$state = $row['field_address_administrative_area'];
      	$zip = $row['field_address_postal_code'];
      	$phone = unserialize($row['field_address_data']);
      }
    endif;
  
    // Get the bundle the addressfield is being used
    $form = $context['instance']['bundle'];

    if ($form == 'owner_update'):
    // Use the created defaults if the entityform is 'owner_update'
      $default_values['thoroughfare'] = $address;
      $default_values['premise'] = $address2;
      $default_values['locality'] = $city;
      $default_values['administrative_area'] = $state;
      $default_values['postal_code'] = $zip;
      $default_values['phone_number'] = $phone['phone_number'];
    endif;
  endif;
}

Status: Fixed » Closed (fixed)

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