You are here

ws_addressfield.module in Web Service Data 7

Add support for addressfield to be used with web service fields

@author Mathew Winstone <mwinstone@coldfrontlabs.ca>

File

modules/ws_addressfield/ws_addressfield.module
View source
<?php

/**
 * @file
 * Add support for addressfield to be used with web service fields
 *
 * @author Mathew Winstone <mwinstone@coldfrontlabs.ca>
 */

/**
 * Implements hook_wsfields_FIELD_TYPE_data_alter().
 */
function ws_addressfield_wsfields_addressfield_data_alter($data, $field) {
  $field_data = array();
  if (!empty($field['translatable'])) {
    $languages = language_list();
  }
  else {
    $languages = array(
      LANGUAGE_NONE => LANGUAGE_NONE,
    );
  }
  foreach ($languages as $lang => $language) {
    foreach ($data[$lang] as $key => $lang_data) {
      $field_data[$lang][$key] = _ws_addressfield_map_address($lang_data);
    }
  }
  $data = $field_data;

  // Save the new field data
  return $data;
}

/**
 * Implements hook_wsconfig_processor_info().
 */
function ws_addressfield_wsconfig_processor_info() {
  return array(
    'ws_addressfield_processor' => array(
      'fields' => array(
        'addressfield' => 'Address Field',
      ),
    ),
  );
}

/**
 * Map the data values to their address field containers
 *
 * Maps values to the following array keys:
 * - country
 * - administrative_area
 * - sub_administrative_area
 * - locality
 * - dependent_locality
 * - postal_code
 * - thoroughfare
 * - premise
 * - sub_premise
 * - organisation_name
 * - name_line
 * - first_name
 * - last_name
 * - data
 *
 * @param array $data [reference]
 *  Data to parse
 * @return array
 *  Returns the structured array
 */
function _ws_addressfield_map_address(&$data) {
  $address = array();

  // @todo replace with call to field['columns'] to get container map
  $address['country'] = isset($data['country']) ? $data['country'] : NULL;
  $address['administrative_area'] = isset($data['administrative_area']) ? $data['administrative_area'] : NULL;
  $address['sub_administrative_area'] = isset($data['sub_administrative_area']) ? $data['sub_administrative_area'] : NULL;
  $address['locality'] = isset($data['locality']) ? $data['locality'] : NULL;
  $address['dependent_locality'] = isset($data['dependent_locality']) ? $data['dependent_locality'] : NULL;
  $address['postal_code'] = isset($data['postal_code']) ? $data['postal_code'] : NULL;
  $address['thoroughfare'] = isset($data['thoroughfare']) ? $data['thoroughfare'] : NULL;
  $address['premise'] = isset($data['premise']) ? $data['premise'] : NULL;
  $address['sub_premise'] = isset($data['sub_premise']) ? $data['sub_premise'] : NULL;
  $address['organisation_name'] = isset($data['organisation_name']) ? $data['organisation_name'] : NULL;
  $address['name_line'] = isset($data['name_line']) ? $data['name_line'] : NULL;
  $address['first_name'] = isset($data['first_name']) ? $data['first_name'] : NULL;
  $address['last_name'] = isset($data['last_name']) ? $data['last_name'] : NULL;
  $address['data'] = isset($data['data']) ? $data['data'] : NULL;
  return $address;
}