You are here

international_phone.module in International Phone Field 7

Same filename and directory in other branches
  1. 7.2 international_phone.module

Allow users to add international phone fields with a entity.

File

international_phone.module
View source
<?php

/**
 * @file
 * Allow users to add international phone fields with a entity.
 */

/**
 * Implements hook_field_info().
 */
function international_phone_field_info() {
  return array(
    'international_phone' => array(
      'label' => t('International Phone'),
      'instance_settings' => array(
        'international_phone_country_code' => 0,
        'international_phone_default_country_code' => '1',
        'international_phone_int_max_length' => 15,
      ),
      'default_formatter' => 'international_phone',
      'default_widget' => 'international_phone_textfield',
      'property_type' => 'text',
    ),
  );
}

/**
 * Implements hook_field_is_empty().
 */
function international_phone_field_is_empty($item, $field) {
  return empty($item['value']);
}

/**
 * Implements hook_field_formatter_info().
 */
function international_phone_field_formatter_info() {
  return array(
    'international_phone' => array(
      'label' => t('Default'),
      'field types' => array(
        'international_phone',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function international_phone_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  foreach ($items as $delta => $item) {
    $text = '';
    if (isset($item['value'])) {
      $text = check_plain($item['value']);

      // iPhone Support.
      if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE) {
        $text = '<a href="tel:' . $text . '">' . $text . '</a>';
      }
    }
    $element[$delta]['#markup'] = $text;
    $element['#attached']['js'][] = drupal_get_path('module', 'international_phone') . '/js/intlTelInput.js';
    $element['#attached']['js'][] = drupal_get_path('module', 'international_phone') . '/js/international_phone.js';
  }
  return $element;
}

/**
 * Implements hook_field_widget_info().
 */
function international_phone_field_widget_info() {
  return array(
    'international_phone_textfield' => array(
      'label' => t('Text field'),
      'field types' => array(
        'international_phone',
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function international_phone_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  drupal_add_css(drupal_get_path('module', 'international_phone') . '/css/intlTelInput.css');
  $element['value'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($element['#title']),
    '#description' => check_plain($element['#description']),
    '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
    '#required' => $element['#required'],
    '#size' => 17,
    '#attributes' => array(
      'class' => array(
        'international_phone-number',
      ),
    ),
  );
  drupal_add_js('http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', 'external');
  drupal_add_js(drupal_get_path('module', 'international_phone') . '/js/intlTelInput.js');
  drupal_add_js(drupal_get_path('module', 'international_phone') . '/js/international_phone.js');
  return $element;
}