View source
<?php
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
function telephone_formatter_field_formatter_info() {
return array(
'telephone_formatter' => array(
'label' => t('Formatted telephone'),
'field types' => array(
'telephone',
),
'settings' => array(
'format' => PhoneNumberFormat::INTERNATIONAL,
'link' => TRUE,
'default_country' => NULL,
),
),
);
}
function telephone_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$elements['format'] = array(
'#type' => 'select',
'#title' => t('Format'),
'#description' => t('List of available formats'),
'#default_value' => $settings['format'],
'#options' => _telephone_formatter_get_available_formats(),
);
$elements['link'] = array(
'#type' => 'checkbox',
'#title' => t('Link'),
'#description' => t('Format as link'),
'#default_value' => $settings['link'],
);
$elements['default_country'] = array(
'#type' => 'select',
'#title' => t('Default country'),
'#description' => t('If field allows internal telephone numbers you can choose which country this number belongs to by default. It is highly advised to enable telephone validation for this field to ensure that telephone number is valid and can be parsed and reformatted.'),
'#default_value' => $settings['default_country'],
'#options' => array(
NULL => t('- Do not use default country -'),
) + _telephone_formatter_get_country_codes(),
);
return $elements;
}
function telephone_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
$formats = _telephone_formatter_get_available_formats();
$summary[] = t('Format: @format', array(
'@format' => $formats[$settings['format']],
));
$summary[] = t('Link: @link', array(
'@link' => $settings['link'] ? t('Yes') : t('No'),
));
if ($default_country = $settings['default_country']) {
$summary[] = t('Default country: @default_country', array(
'@default_country' => $default_country,
));
}
return implode('<br>', $summary);
}
function telephone_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
foreach ($items as $delta => $item) {
try {
if ($settings['link']) {
$element[$delta] = array(
'#markup' => _telephone_formatter_view_link_value($item, $settings),
);
}
else {
$element[$delta] = array(
'#markup' => _telephone_formatter_view_formatted_value($item, $settings),
);
}
} catch (\Exception $e) {
$element[$delta] = array(
'#markup' => $item['value'],
);
}
}
return $element;
}
function _telephone_formatter_view_link_value($item, $settings) {
$formatted_value = _telephone_formatter_get_formatted_value($item['value'], $settings['format'], $settings['default_country']);
$uri = _telephone_formatter_get_formatted_value($formatted_value, PhoneNumberFormat::RFC3966, $settings['default_country']);
$value = l($formatted_value, $uri, array(
'external' => TRUE,
));
return $value;
}
function _telephone_formatter_view_formatted_value($item, $settings) {
return _telephone_formatter_get_formatted_value($item['value'], $settings['format'], $settings['default_country']);
}
function _telephone_formatter_get_formatted_value($input, $format, $region = NULL) {
$phone_util = PhoneNumberUtil::getInstance();
$number = $phone_util
->parse($input, $region);
if (!$phone_util
->isValidNumber($number)) {
throw new \InvalidArgumentException('Number is invalid.');
}
$value = $phone_util
->format($number, $format);
return $value;
}
function _telephone_formatter_get_available_formats() {
return array(
PhoneNumberFormat::INTERNATIONAL => t('International'),
PhoneNumberFormat::E164 => t('E164'),
PhoneNumberFormat::NATIONAL => t('National'),
PhoneNumberFormat::RFC3966 => t('RFC3966'),
);
}
function _telephone_formatter_get_country_codes() {
include_once DRUPAL_ROOT . '/includes/iso.inc';
$phone_util = PhoneNumberUtil::getInstance();
$regions = array();
foreach (_country_get_predefined_list() as $region => $name) {
$region_meta = $phone_util
->getMetadataForRegion($region);
if (is_object($region_meta)) {
$regions[$region] = $name . ' - +' . $region_meta
->getCountryCode() . ' ' . $region_meta
->getLeadingDigits();
}
}
return $regions;
}