You are here

field_nif.module in Field NIF 7

File

field_nif.module
View source
<?php

/*
 * @file
 * Module file for NIF/CIF/NIE fields.
 */

/**
 * Implements hook_field_info().
 */
function field_nif_field_info() {
  return array(
    'nif' => array(
      'label' => t('NIF/CIF/NIE'),
      'description' => t('Stores and validates an NIF/CIF/NIE field.'),
      'default_widget' => 'nif_data',
      'default_formatter' => 'nif_default',
      'instance_settings' => array(
        'supported_numbers' => array(
          'nif',
          'cif',
          'nie',
        ),
      ),
      'property_type' => 'nif',
      'property_callbacks' => array(
        'field_nif_property_info_callback',
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_info().
 */
function field_nif_field_widget_info() {
  return array(
    'nif_data' => array(
      'label' => t('NIF/CIF/NIE'),
      'field types' => array(
        'nif',
      ),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function field_nif_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($instance['widget']['type'] == 'nif_data') {

    // Add a virtual element that handles the complete nif number.
    $element['nif'] = array(
      '#type' => 'textfield',
      '#title' => $element['#title'],
      '#description' => $element['#description'],
      '#default_value' => isset($items[$delta]['nif']) ? $items[$delta]['nif'] : '',
      '#required' => $instance['required'] && ($delta == 0 || $field['cardinality'] > 0),
      '#size' => 10,
    );

    // Add the elements stored in db.
    $element['number'] = array(
      '#type' => 'hidden',
    );
    $element['first_letter'] = array(
      '#type' => 'hidden',
    );
    $element['last_letter'] = array(
      '#type' => 'hidden',
    );
    $element['type'] = array(
      '#type' => 'hidden',
    );
    if (module_exists('i18n_field')) {
      $instance_current = i18n_string_object_translate('field_instance', $instance);

      // Translate field title and description if set and it is the default one.
      if (!empty($instance_current['label']) && $instance_current['label'] != $instance['label']) {
        $element['nif']['#title'] = $instance_current['label'];
      }
      if (!empty($instance_current['description']) && $instance_current['description'] != $instance['description']) {
        $element['nif']['#description'] = $instance_current['description'];
      }
    }
  }

  // Add a validation function
  $element['#element_validate'][] = 'field_nif_validate_nif_number';

  // Expose the instance settings to the validator.
  $form_state['instance'] = $instance;
  return $element;
}

/**
 * Validate handler to check if the number is correct and store the values
 * splitted into their correct fields in the database.
 */
function field_nif_validate_nif_number($element, &$form_state) {
  if (!empty($element['nif']['#value'])) {
    if (strlen($element['nif']['#value']) != 9) {
      form_error($element['nif'], t('NIF/CIF/NIE number needs to be 9 characters long'));
    }
    else {

      // Load the utilities for getting the NIF splitted.
      module_load_include('inc', 'field_nif', 'field_nif.utils');
      $nif_parts = _field_nif_validate_dni_cif_nie($element['nif']['#value'], $form_state['instance']['settings']['supported_numbers']);
      if ($nif_parts) {
        form_set_value($element['number'], $nif_parts['number'], $form_state);
        form_set_value($element['first_letter'], isset($nif_parts['first_letter']) ? $nif_parts['first_letter'] : '', $form_state);
        form_set_value($element['last_letter'], isset($nif_parts['last_letter']) ? $nif_parts['last_letter'] : '', $form_state);
        form_set_value($element['type'], isset($nif_parts['type']) ? $nif_parts['type'] : '', $form_state);
      }
      else {
        form_error($element['nif'], t('NIF/CIF/NIE number is not correct'));
      }
    }
  }
}

/**
 * Implement hook_field_formatter_info().
 */
function field_nif_field_formatter_info() {
  return array(
    'nif_default' => array(
      'label' => t('NIF Field formatter'),
      'field types' => array(
        'nif',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function field_nif_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  foreach ($items as $delta => $item) {
    if ($display['type'] == 'nif_default') {
      $element[$delta] = array(
        '#markup' => check_plain($item['nif']),
      );
    }
  }
  return $element;
}

/**
 * Implement hook_field_is_empty().
 */
function field_nif_field_is_empty($item, $field) {
  return empty($item['number']);
}

/**
 * Implements hook_field_load().
 */
function field_nif_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => $item) {

      // Load and display the correct information from the db.
      $items[$id][$delta]['nif'] = $item['first_letter'] . $item['number'] . $item['last_letter'];
    }
  }
}

/**
 * Implements hook_field_instance_settings_form().
 */
function field_nif_field_instance_settings_form($field, $instance) {
  $settings = $instance['settings'];
  $form['supported_numbers'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Supported id number types'),
    '#default_value' => $settings['supported_numbers'],
    '#options' => array(
      'nif' => t('NIF'),
      'cif' => t('CIF'),
      'nie' => t('NIE'),
    ),
  );
  return $form;
}

/**
 * Callback to alter the property info of nif fields.
 *
 * @see field_nif_field_info().
 */
function field_nif_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  $property =& $info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  $property['property info'] = field_nif_property_field_item_nif_info($field_type);
  unset($property['query callback']);
}

/**
 * Defines the data structure used for field nif items.
 */
function field_nif_property_field_item_nif_info($field_type) {
  return array(
    'nif' => array(
      'type' => 'string',
      'label' => $field_type['label'],
      'description' => t('The NIF/CIF/DNI number.'),
      'getter callback' => 'field_nif_property_get',
    ),
  );
}

/**
 * Property getter callback returning complete nifs.
 */
function field_nif_property_get($data, array $options, $name, $type, $info) {
  return $data['first_letter'] . $data['number'] . $data['last_letter'];
}

Functions

Namesort descending Description
field_nif_field_formatter_info Implement hook_field_formatter_info().
field_nif_field_formatter_view Implements hook_field_formatter_view().
field_nif_field_info Implements hook_field_info().
field_nif_field_instance_settings_form Implements hook_field_instance_settings_form().
field_nif_field_is_empty Implement hook_field_is_empty().
field_nif_field_load Implements hook_field_load().
field_nif_field_widget_form Implements hook_field_widget_form().
field_nif_field_widget_info Implements hook_field_widget_info().
field_nif_property_field_item_nif_info Defines the data structure used for field nif items.
field_nif_property_get Property getter callback returning complete nifs.
field_nif_property_info_callback Callback to alter the property info of nif fields.
field_nif_validate_nif_number Validate handler to check if the number is correct and store the values splitted into their correct fields in the database.