You are here

function _phone_migrate_phone_number in Phone 7.2

Helper function for migrate-type functions: convert a number from a single-entry number to country code, number, and extension.

The main difference between the processing here and in phone_element_validate() is that if there is "bad" data, it is is allowed through without processing.

3 calls to _phone_migrate_phone_number()
phone_content_migrate_data_record_alter in ./phone.content_migrate.inc
Implements hook_content_migrate_field_alter().
phone_feeds_set_target in ./phone.feeds.inc
Callback for mapping. Here is where the actual mapping happens.
phone_update_7200 in ./phone.install

File

./phone.module, line 1190
The phone module lets administrators use a phone number field type.

Code

function _phone_migrate_phone_number($number, $default_country = NULL) {
  phone_libphonenumber();

  // Can't process an empty number
  if (empty($number)) {
    return array();
  }
  if (!isset($default_country)) {
    $default_country = variable_get('site_default_country', 'US');
  }

  // No need for drupal_strtoupper() here, as country code should be ASCII only.
  $default_country = strtoupper($default_country);
  list($phone_util, $processed_phone) = _phone_libphonenumber($number, $default_country, '');

  // If there was any type of error processing the old phone number,
  // simply stuff the existing value into number so that information is
  // not lost.
  if (empty($processed_phone)) {
    return array(
      'number' => $number,
    );
  }
  $item = array();

  // Not doing any validation checks here, because there's no way to get
  // user to fix the problem, and it's better to keep invalid info than
  // discard it.
  $countrycode = $phone_util
    ->getRegionCodeForNumber($processed_phone);
  if (!empty($countrycode)) {
    $item['countrycode'] = $countrycode;
    $item['number'] = $processed_phone
      ->getNationalNumber();
    if ($processed_phone
      ->getExtension()) {
      $item['extension'] = $processed_phone
        ->getExtension();
    }
  }
  else {

    // If country code could not be determined, treat as another error.
    $item['number'] = $number;
  }
  return $item;
}