You are here

country_field.module in Country Field 7

Contains field hook implementations for a country select field.

File

country_field.module
View source
<?php

/**
 * @file
 * Contains field hook implementations for a country select field.
 */

/**
 * Implements hook_field_info().
 */
function country_field_field_info() {
  return array(
    'country' => array(
      'label' => t('Country'),
      'description' => t("This field allows you to select a country."),
      'settings' => array(
        'allowed_values' => array(),
        'allowed_values_function' => 'country_field_countries',
      ),
      'instance_settings' => array(
        'countries' => array(),
      ),
      'default_widget' => 'options_select',
      'default_formatter' => 'list_default',
    ),
  );
}

/**
 * Implements hook_field_widget_info_alter().
 */
function country_field_field_widget_info_alter(&$info) {
  $info['options_select']['field types'][] = 'country';
  $info['options_buttons']['field types'][] = 'country';
}

/**
 * Implements hook_field_formatter_info_alter().
 */
function country_field_field_formatter_info_alter(&$info) {
  $info['list_default']['field types'][] = 'country';
}

/**
 * Implements hook_field_instance_settings_form().
 */
function country_field_field_instance_settings_form($field, $instance) {
  return array(
    'countries' => array(
      '#type' => 'select',
      '#title' => t('Limit to countries'),
      '#description' => t('Select a list of countries to be exposed as field options. If none is selected, this field instance will expose all countries.'),
      '#default_value' => $instance['settings']['countries'],
      '#options' => _country_field_countries(),
      '#multiple' => TRUE,
      '#size' => 8,
    ),
  );
}

/**
 * Implements hook_options_list().
 */
function country_field_options_list($field, $instance, $entity_type, $entity) {
  return country_field_countries($field, $instance, $entity_type, $entity, $cacheable);
}

/**
 * Implements hook_field_is_empty().
 */
function country_field_field_is_empty($item, $field) {
  if (!is_array($item) || empty($item['value'])) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Returns a list of countries.
 */
function country_field_countries($field, $instance, $entity_type, $entity, &$cacheable) {
  $cacheable = TRUE;
  $countries = _country_field_countries();
  if (!empty($instance['settings']['countries'])) {
    $countries = array_intersect_key($countries, drupal_map_assoc($instance['settings']['countries']));
  }
  return $countries;
}

/**
 * Provides a list of countries.
 *
 * @return array
 *   Associative array keyed by country code and having the country name as
 *   values.
 */
function _country_field_countries() {
  static $countries;
  if (!isset($countries)) {
    require_once DRUPAL_ROOT . '/includes/locale.inc';
    $countries = country_get_list();
  }
  return $countries;
}

/**
 * Implements hook_views_api().
 */
function country_field_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'country_field') . '/views',
  );
}