You are here

function phone_field_formatter_view in Phone 7.2

Same name and namespace in other branches
  1. 7 phone.module \phone_field_formatter_view()

Implements hook_field_formatter_view().

File

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

Code

function phone_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

  // Load libphonenumber.
  phone_libphonenumber();
  $element = array();
  $settings = $display['settings'];
  $formatter = $display['type'];
  $allowed_values = phone_numbertype_allowed_values($field, $instance);
  $components = array_filter($settings['components']);
  foreach ($items as $delta => $item) {
    $number = $item['number'];
    $countrycode = $item['countrycode'];
    $extension = $item['extension'];
    $numbertype = $item['numbertype'];
    $numbertype_id = ' phone-numbertype-option-' . drupal_html_class($numbertype);

    // Provide access to a nicer label.
    $numbertype_label = isset($allowed_values[$item['numbertype']]) ? $allowed_values[$item['numbertype']] : $item['numbertype'];

    // If the extension is disabled, don't pass one through if data exists.
    if (!$instance['settings']['enable_extension'] || !isset($components['extension'])) {
      $extension = '';
    }

    // If the number type is disabled, don't pass one through if data exists.
    if (!$field['settings']['enable_numbertype'] || !isset($components['numbertype'])) {
      $numbertype = '';
      $numbertype_label = '';
      $numbertype_id = '';
    }
    $formatted_number = phone_libphonenumber_format($number, $countrycode, $extension, $formatter, $settings['allow_alpha'], $settings['extension_prefix']);
    $link = $settings['as_tel_link'];
    $href = 'tel:' . phone_libphonenumber_format($number, $countrycode, $extension, 'phone_rfc3966');
    $numbertype_attributes = array();
    if ($settings['full_hcard']) {
      $numbertype_attributes = array(
        'title' => $numbertype,
      );
    }

    // Create a render array for our item.
    $numbertype_before = $settings['numbertype_position'] == 'before';
    $render = array(
      'phone' => array(
        '#type' => 'container',
        '#attributes' => array(
          'class' => array(
            'tel',
            'phone-container' . $numbertype_id,
          ),
        ),
        'numbertype' => array(
          '#access' => !empty($numbertype),
          '#type' => 'html_tag',
          '#tag' => $settings['full_hcard'] ? 'abbr' : 'span',
          '#attributes' => $numbertype_attributes + array(
            'class' => array(
              'type',
              'phone-numbertype',
            ),
          ),
          '#value' => empty($numbertype_label) ? $numbertype : $numbertype_label,
          '#value_suffix' => $numbertype_before ? ': ' : '',
          '#weight' => $numbertype_before ? -2 : 2,
        ),
        'number' => array(
          '#type' => $link ? 'link' : 'html_tag',
          '#tag' => 'span',
          '#attributes' => array(
            'class' => array(
              'value',
              'phone-number',
            ),
          ),
          '#value' => $formatted_number,
          '#title' => $formatted_number,
          '#href' => $href,
          '#weight' => 0,
        ),
        'country' => array(
          '#access' => isset($components['country']),
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => array(
            'class' => array(
              'phone-country',
            ),
          ),
          '#value' => phone_countries($countrycode, 'country'),
          '#value_prefix' => '(',
          '#value_suffix' => ')',
          '#weight' => $settings['country_name_position'] == 'before' ? -1 : 1,
        ),
      ),
    );
    $element[$delta] = $render;
  }
  return $element;
}