Our site's user base is primarily part of the default country (US), however we do have a few users with phone numbers for other countries. It doesn't make sense to format all of the phone numbers with +1 for the handful of non-US cases and setting everything to national causes confusion when looking at the few international numbers as the country code is needed in those instances. To deal with this I've developed a formatting option that checks against the default country and if it's the same as the country code for the phone number then the formatting function is passed phone_national, otherwise it gets phone_international.

Attached is a patch that is the first step for this. It still needs some more work on the settings form to require a default country if this formatter is selected, but I wanted to share what I have.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ejustice’s picture

I've added a check to ensure that 'Enable default country code' is set and if it isn't then the code is setting the formatter to phone_international. Additionally I've got phone_field_formatter_settings_summary() checking to see if 'Enable default country code' is checked and if not a note is being put into the information displayed in the summary to tell the user that all numbers will be formatted using phone_interntional unless a default country code is set.

I don't think this is the ideal way to handle this however I haven't found a way to prevent phone_field_formatter_info() from presenting phone_national_international as an option as that hook doesn't have any information about the widgets that are using it and the function is cached long before the field display page is being loaded, so what is correct for one instance may not be for another.

ejustice’s picture

I found a bug with the code in #2 where a second phone number for the same user will always be formatted as international. Attached is a patch without that issue.

cweagans’s picture

Status: Active » Needs work

This looks pretty good, but before we commit this, can you remove trailing whitespace please?

ejustice’s picture

Anonymous’s picture

Thanks for this.

In order to get this to work, I had to make the following changes:

if ($display['type'] == 'phone_national_international' && !$instance['widget']['settings']['country_options']['enable_default_country']) {
      $summary[] = t('A default country code has not been set for this field.  This formatter requires a default country code. All phone numbers will be formatted using the International Formatter.');
  }

changed to:

if ($display['type'] == 'phone_national_international' && !$instance['settings']['country_options']['enable_default_country']) {
      $summary[] = t('A default country code has not been set for this field.  This formatter requires a default country code. All phone numbers will be formatted using the International Formatter.');
  }

&

if ($formatter == 'phone_national_international' && $instance['widget']['settings']['country_options']['enable_default_country']) {
    $default_country = $instance['widget']['settings']['country_options']['default_country'];
  }
  else {
    $formatter = 'phone_international';
  }

changed to:

if ($formatter == 'phone_national_international' && $instance['settings']['country_options']['enable_default_country']) {
    $default_country = $instance['settings']['country_options']['default_country'];
  }
  else {
    $formatter = 'phone_international';
  }

i.e., remove '['widget']'.

In addition, I changed the following:

if ($formatter == 'phone_national_international' && $default_country == $countrycode) {
      $formatted_number = phone_libphonenumber_format($number, $countrycode, $extension, 'phone_national', $settings['allow_alpha']);
    }
    else if ($formatter == 'phone_national_international') {
      $formatted_number = phone_libphonenumber_format($number, $countrycode, $extension, 'phone_international', $settings['allow_alpha']);
    }
    else {
      $formatted_number = phone_libphonenumber_format($number, $countrycode, $extension, $formatter, $settings['allow_alpha']);
    }

to

if ($formatter == 'phone_national_international' && $default_country == $countrycode) {
      $formatted_number = phone_libphonenumber_format($number, $countrycode, $extension, 'phone_national', $settings['allow_alpha'], $settings['extension_prefix']);
    }
    else if ($formatter == 'phone_national_international') {
      $formatted_number = phone_libphonenumber_format($number, $countrycode, $extension, 'phone_international', $settings['allow_alpha'], $settings['extension_prefix']);
    }
    else {
      $formatted_number = phone_libphonenumber_format($number, $countrycode, $extension, $formatter, $settings['allow_alpha'], $settings['extension_prefix']);
    }

i.e., I added $settings['extension_prefix'] to the parameters.

I applied these manually since the patch failed against the latest dev. I hope this report is helpful.