vat_number.module in VAT Number 7
Define a VAT Number field for use on Drupal Commerce Customer Profiles.
File
vat_number.moduleView source
<?php
require_once drupal_get_path('module', 'vat_number') . '/vat_number.inc';
/**
* @file
* Define a VAT Number field for use on Drupal Commerce Customer Profiles.
*
*/
/**
* Implements hook_field_info().
*/
function vat_number_field_info() {
return array(
'vat_number' => array(
'label' => t('VAT Number'),
'description' => t('This field stores a VAT number and validates it against the SOAP service.'),
'default_widget' => 'vat_number_widget',
'default_formatter' => 'vat_number_formatter',
// Integrate with the Entity Metadata module.
'property_type' => 'text',
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function vat_number_field_formatter_info() {
return array(
// This formatter just displays the text value.
'vat_number_formatter' => array(
'label' => t('Simple text-based formatter'),
'field types' => array(
'vat_number',
),
),
);
}
/**
* Implements hook_field_widget_info().
*/
function vat_number_field_widget_info() {
return array(
'vat_number_widget' => array(
'label' => t('VAT textfield'),
'settings' => array(
'skip_validation_on_service_failure' => FALSE,
),
'field types' => array(
'vat_number',
),
),
);
}
/**
* Implements hook_field_widget_settings_form().
*/
function vat_number_field_widget_settings_form($field, $instance) {
$widget = $instance['widget'];
$settings = $widget['settings'];
$form = array();
if ($widget['type'] == 'vat_number_widget') {
$form['skip_validation_on_service_failure'] = array(
'#type' => 'checkbox',
'#title' => t('Soft validation'),
'#description' => t('Skip validation against EU service if it is having troubles.'),
'#default_value' => $settings['skip_validation_on_service_failure'],
);
}
return $form;
}
/**
* Implements hook_field_formatter_view().
*/
function vat_number_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
// This formatter simply outputs the field as text.
case 'vat_number_formatter':
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#markup' => $item['value'],
);
}
break;
}
return $element;
}
/**
* Implements hook_field_widget_form().
*/
function vat_number_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$value = isset($items[$delta]['value']) ? $items[$delta]['value'] : '';
$base = $element;
switch ($instance['widget']['type']) {
case 'vat_number_widget':
$element['value'] = array(
'#type' => 'textfield',
'#default_value' => $value,
'#size' => 20,
'#maxlength' => 255,
'#element_validate' => array(
'vat_number_field_widget_element_validate',
),
) + $base;
break;
}
return $element;
}
/**
* Custom validation callback for vat_number_field_widget_form()
* @param array $element
* @param array $form_state
*/
function vat_number_field_widget_element_validate($element, &$form_state) {
//$field = field_widget_field($element, $form_state);
// Find out are we tending to skip validation if service is having troubles.
$instance = field_widget_instance($element, $form_state);
$skip_validation_on_service_failure = $instance['widget']['settings']['skip_validation_on_service_failure'];
$value = $element['#value'];
$message = '';
// Reject invalid characters.
if (!empty($value)) {
//we want all messages inside the form so we create them here
// 1: Check structure, 2: Check validity
//at first check the format of VAT, if its ok ...
$vat_format_check = _vat_number_check_vat_format($value);
if (isset($vat_format_check['message'])) {
$message = $vat_format_check['message'];
}
elseif (_vat_number_validate_vat($value, $skip_validation_on_service_failure) === FALSE) {
$url_vies = 'http://ec.europa.eu/taxation_customs/vies/';
$link_vies = l(t('the European Union\'s VAT Information Exchange System'), $url_vies, array(
'attributes' => array(
'target' => '_blank',
'title' => $url_vies,
),
));
$placeholders = array(
'!link_vies' => $link_vies,
);
$message .= t('The provided VAT ID number is invalid according to !link_vies.<br />Please ensure to enter your correct VAT ID number!', $placeholders);
}
if (!empty($message)) {
form_error($element, $message);
}
}
}
/**
* Implements hook_field_is_empty().
*
* hook_field_is_emtpy() is where Drupal asks us if this field is empty.
* Return TRUE if it does NOT contain data, FALSE if it does. This lets
* the form API flag an error when required fields are empty.
*/
function vat_number_field_is_empty($item, $field) {
if (empty($item['value']) && (string) $item['value'] !== '0') {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_field_validate().
*/
function vat_number_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
//@todo wee need this? we use a custom valid function, is it enough? seems so?
}
Functions
Name | Description |
---|---|
vat_number_field_formatter_info | Implements hook_field_formatter_info(). |
vat_number_field_formatter_view | Implements hook_field_formatter_view(). |
vat_number_field_info | Implements hook_field_info(). |
vat_number_field_is_empty | Implements hook_field_is_empty(). |
vat_number_field_validate | Implements hook_field_validate(). |
vat_number_field_widget_element_validate | Custom validation callback for vat_number_field_widget_form() |
vat_number_field_widget_form | Implements hook_field_widget_form(). |
vat_number_field_widget_info | Implements hook_field_widget_info(). |
vat_number_field_widget_settings_form | Implements hook_field_widget_settings_form(). |