You are here

function addressfield_phone_field_attach_load in Address Field Phone 7

Implements hook_field_attach_load().

Load and set additional addressfield field values from serialized data column.

File

./addressfield_phone.module, line 111
Add additional phone fields to addressfield.

Code

function addressfield_phone_field_attach_load($entity_type, $entities, $age, $options) {
  $addressfields = addressfield_phone_addressfield_types();
  $phone_elements = drupal_map_assoc(array(
    'phone_number',
    'phone_number_extension',
    'mobile_number',
    'fax_number',
  ));
  foreach ($entities as $entity) {
    foreach ($addressfields as $field_name) {
      if (empty($entity->{$field_name})) {
        continue;
      }
      foreach ($entity->{$field_name} as $lang_code => $fields) {
        foreach ($fields as $delta => $fieldset) {

          // Fill default values for pre-addressfield_phone addresses to keep
          // Commerce Order from duplicating address.
          if (empty($fieldset['data'])) {
            $fieldset['data'] = serialize(array_fill_keys($phone_elements, ''));
          }

          // Unserialize data, else skip if string
          if (($data = unserialize($fieldset['data'])) && is_array($data)) {

            // Store unserialized data values in additional fields
            $phone_data = array_intersect_key($data, $phone_elements);
            $entity->{$field_name}[$lang_code][$delta] = array_merge($fieldset, $phone_data);
          }
        }
      }
    }
  }
}