You are here

function addressfield_phone_field_attach_presave in Address Field Phone 7

Implements hook_field_attach_presave().

Store additional address fields serialized in addressfield data column.

File

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

Code

function addressfield_phone_field_attach_presave($entity_type, $entity) {
  $addressfields = addressfield_phone_addressfield_types();
  foreach ($addressfields as $field_name) {
    if (isset($entity->{$field_name})) {
      $field = field_info_field($field_name);
      list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
      $instance = field_info_instance($entity_type, $field_name, $bundle);
      $default_fields = array_keys(addressfield_default_values($field, $instance));
      $default_fields[] = 'element_key';
      foreach ($entity->{$field_name} as $lang_code => $fields) {
        foreach ($fields as $delta => $fieldset) {
          $data = array();
          foreach ($fieldset as $field_key => $field_value) {

            // Compare fields with addressfield default fields and store any
            // additional fields in data.
            if (!in_array($field_key, $default_fields)) {

              // Store additional field value
              $data[$field_key] = $field_value;
            }
          }
          if (!empty($data)) {
            $entity->{$field_name}[$lang_code][$delta]['data'] = serialize($data);
          }
        }
      }
    }
  }
}