You are here

function addressfield_field_presave in Address Field 7

Implements hook_field_presave().

1 call to addressfield_field_presave()
addressfield_field_load in ./addressfield.module
Implements hook_field_load().

File

./addressfield.module, line 479
Defines a field for attaching country-specific addresses to entities.

Code

function addressfield_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  foreach ($items as $delta => &$item) {

    // If the first name and last name are set but the name line isn't...
    if (isset($item['first_name']) && isset($item['last_name']) && !isset($item['name_line'])) {

      // Combine the first and last name to be the name line.
      $items[$delta]['name_line'] = $items[$delta]['first_name'] . ' ' . $items[$delta]['last_name'];
    }
    elseif (isset($item['name_line'])) {

      // Otherwise if the name line is set, separate it out into a best guess at
      // the first and last name.
      $names = explode(' ', $item['name_line']);
      $item['first_name'] = array_shift($names);
      $item['last_name'] = implode(' ', $names);
    }

    // Trim whitespace from all of the address components and convert any double
    // spaces to single spaces.
    foreach ($item as $key => &$value) {
      if (!in_array($key, array(
        'data',
      )) && is_string($value)) {
        $value = trim(preg_replace('/[[:blank:]]{2,}/u', ' ', $value));
      }
    }
  }
}