View source
<?php
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',
),
),
);
}
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,
),
),
);
}
function field_nif_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
if ($instance['widget']['type'] == 'nif_data') {
$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,
);
$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);
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'];
}
}
}
$element['#element_validate'][] = 'field_nif_validate_nif_number';
$form_state['instance'] = $instance;
return $element;
}
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 {
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'));
}
}
}
}
function field_nif_field_formatter_info() {
return array(
'nif_default' => array(
'label' => t('NIF Field formatter'),
'field types' => array(
'nif',
),
),
);
}
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;
}
function field_nif_field_is_empty($item, $field) {
return empty($item['number']);
}
function field_nif_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
$items[$id][$delta]['nif'] = $item['first_letter'] . $item['number'] . $item['last_letter'];
}
}
}
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;
}
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']);
}
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',
),
);
}
function field_nif_property_get($data, array $options, $name, $type, $info) {
return $data['first_letter'] . $data['number'] . $data['last_letter'];
}