You are here

function mobile_number_field_formatter_view in Mobile Number 7

Implements hook_field_formatter_view().

File

./mobile_number.module, line 287
mobile_number.module

Code

function mobile_number_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  foreach ($items as $delta => $item) {
    $output = array();
    if (isset($item['value'])) {
      try {
        $as_link = !empty($settings['as_link']) ? TRUE : FALSE;
        $mobile_number = new MobileNumber($item['value'], NULL, array());
        switch ($display['type']) {
          case 'mobile_number_international':
            if ($as_link) {
              $output = array(
                '#type' => 'link',
                '#title' => $mobile_number->libUtil
                  ->format($mobile_number->libPhoneNumber, 1),
                '#href' => "tel:{$mobile_number->callableNumber}",
              );
            }
            else {
              $output = array(
                '#type' => 'markup',
                '#markup' => check_plain($mobile_number->libUtil
                  ->format($mobile_number->libPhoneNumber, 1)),
              );
            }
            break;
          case 'mobile_number_country':
            if ($settings['type'] == 'code') {
              $output = array(
                '#type' => 'markup',
                '#markup' => check_plain($mobile_number->country),
              );
            }
            else {
              $output = array(
                '#type' => 'markup',
                '#markup' => check_plain(MobileNumber::getCountryName($mobile_number->country)),
              );
            }
            break;
          case 'mobile_number_local':
            if ($as_link) {
              $output = array(
                '#type' => 'link',
                '#title' => $mobile_number->libUtil
                  ->format($mobile_number->libPhoneNumber, 2),
                '#href' => "tel:{$mobile_number->callableNumber}",
              );
            }
            else {
              $output = array(
                '#type' => 'markup',
                '#markup' => check_plain($mobile_number->libUtil
                  ->format($mobile_number->libPhoneNumber, 2)),
              );
            }
            break;
          case 'mobile_number_verified':
            $output = array(
              '#type' => 'markup',
              '#markup' => '<span class="verified-status ' . (!empty($item['verified']) ? 'verified' : '') . '">' . check_plain(!empty($item['verified']) ? t('Verified') : t('Not verified')) . '</span>',
            );
            break;
        }
      } catch (Exception $e) {
        $output = array(
          '#markup' => t('Invalid number'),
        );
      }
    }
    $element[$delta] = $output;
  }
  return $element;
}