bigint.module in Big Integer 7
Defines numeric field types.
File
bigint.moduleView source
<?php
/**
* @file
* Defines numeric field types.
*/
/**
* Implements hook_help().
*/
function bigint_help($path, $arg) {
switch ($path) {
case 'admin/help#bigint':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The BigInt module defines a big integer which is twice the size of the default integer provided by the core drupal integer field. Number fields can be limited to a specific set of input values or to a range of values. See the <a href="@field-help">Field module help page</a> for more information about fields.', array(
'@field-help' => url('admin/help/field'),
)) . '</p>';
return $output;
}
}
/**
* Implements hook_field_info().
*/
function bigint_field_info() {
return array(
'number_bigint' => array(
'label' => t('BigInt'),
'description' => t('This field stores a number in the database as an bigint.'),
'instance_settings' => array(
'size' => '60',
'min' => '',
'max' => '',
'prefix' => '',
'suffix' => '',
),
'default_widget' => 'number_bigint',
'default_formatter' => 'number_bigint',
'property_type' => 'integer',
),
);
}
/**
* Implements hook_field_settings_form().
*/
function bigint_field_settings_form($field, $instance, $has_data) {
$settings = $field['settings'];
$form = array();
return $form;
}
/**
* Implements hook_field_instance_settings_form().
*/
function bigint_field_instance_settings_form($field, $instance) {
$settings = $instance['settings'];
$form['size'] = array(
'#type' => 'textfield',
'#title' => t('Size of textfield'),
'#default_value' => $settings['size'],
'#required' => TRUE,
'#element_validate' => array(
'element_validate_integer_positive',
),
);
$form['min'] = array(
'#type' => 'textfield',
'#title' => t('Minimum'),
'#default_value' => $settings['min'],
'#description' => t('The minimum value that should be allowed in this field. Leave blank for no minimum.'),
'#element_validate' => array(
'element_validate_number',
),
);
$form['max'] = array(
'#type' => 'textfield',
'#title' => t('Maximum'),
'#default_value' => $settings['max'],
'#description' => t('The maximum value that should be allowed in this field. Leave blank for no maximum.'),
'#element_validate' => array(
'element_validate_number',
),
);
$form['prefix'] = array(
'#type' => 'textfield',
'#title' => t('Prefix'),
'#default_value' => $settings['prefix'],
'#size' => 60,
'#description' => t("Define a string that should be prefixed to the value, like '\$ ' or '€ '. Leave blank for none. Separate singular and plural values with a pipe ('pound|pounds')."),
);
$form['suffix'] = array(
'#type' => 'textfield',
'#title' => t('Suffix'),
'#default_value' => $settings['suffix'],
'#size' => 60,
'#description' => t("Define a string that should be suffixed to the value, like ' m', ' kb/s'. Leave blank for none. Separate singular and plural values with a pipe ('pound|pounds')."),
);
return $form;
}
/**
* Implements hook_field_validate().
*
* Possible error codes:
* - 'number_min': The value is less than the allowed minimum value.
* - 'number_max': The value is greater than the allowed maximum value.
*/
function bigint_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if ($item['value'] != '') {
if (is_numeric($instance['settings']['min']) && $item['value'] < $instance['settings']['min']) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'number_min',
'message' => t('%name: the value may be no less than %min.', array(
'%name' => $instance['label'],
'%min' => $instance['settings']['min'],
)),
);
}
if (is_numeric($instance['settings']['max']) && $item['value'] > $instance['settings']['max']) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'number_max',
'message' => t('%name: the value may be no greater than %max.', array(
'%name' => $instance['label'],
'%max' => $instance['settings']['max'],
)),
);
}
//BigInt's can be no greater than 19 digits, so we hard code it here making it impossible to break the sql saving.
if (strlen($item['value']) > 19) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'number_max',
'message' => t('%name: the length may be no greater than 19 digits.', array(
'%name' => $instance['label'],
'%max' => $instance['settings']['max'],
)),
);
}
}
}
}
/**
* Implements hook_field_presave().
*/
function bigint_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
}
/**
* Implements hook_field_is_empty().
*/
function bigint_field_is_empty($item, $field) {
if (empty($item['value']) && (string) $item['value'] !== '0') {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_field_formatter_info().
*/
function bigint_field_formatter_info() {
return array(
'number_bigint' => array(
'label' => t('Default'),
'field types' => array(
'number_bigint',
),
'settings' => array(
'thousand_separator' => ',',
'decimal_separator' => '.',
'scale' => 0,
'prefix_suffix' => TRUE,
),
),
'number_unformatted' => array(
'label' => t('Unformatted'),
'field types' => array(
'number_bigint',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function bigint_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
if ($display['type'] == 'number_bigint') {
$options = array(
'' => t('<none>'),
'.' => t('Decimal point'),
',' => t('Comma'),
' ' => t('Space'),
"'" => t('Apostrophe'),
);
$element['thousand_separator'] = array(
'#type' => 'select',
'#title' => t('Thousand marker'),
'#options' => $options,
'#default_value' => $settings['thousand_separator'],
);
$element['prefix_suffix'] = array(
'#type' => 'checkbox',
'#title' => t('Display prefix and suffix.'),
'#default_value' => $settings['prefix_suffix'],
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function bigint_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
if ($display['type'] == 'number_bigint') {
$summary[] = number_format(1234.123456789, $settings['scale'], $settings['decimal_separator'], $settings['thousand_separator']);
if ($settings['prefix_suffix']) {
$summary[] = t('Display with prefix and suffix.');
}
}
return implode('<br />', $summary);
}
function bigint_number_format($number, $sep2 = '') {
//The number_format() function in PHP cannot handle large numbers. An example is: 1234567890123456789 would get formated to: 1,234,567,890,123,456,768
//which is obviously incorrect. This function replaces the number_format() function with something that actually works.
return preg_replace("/(?<=\\d)(?=(\\d{3})+(?!\\d))/", $sep2, $number);
}
/**
* Implements hook_field_formatter_view().
*/
function bigint_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
switch ($display['type']) {
case 'number_bigint':
foreach ($items as $delta => $item) {
$output = bigint_number_format($item['value'], $settings['thousand_separator']);
if ($settings['prefix_suffix']) {
$prefixes = isset($instance['settings']['prefix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['prefix'])) : array(
'',
);
$suffixes = isset($instance['settings']['suffix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['suffix'])) : array(
'',
);
$prefix = count($prefixes) > 1 ? format_plural($item['value'], $prefixes[0], $prefixes[1]) : $prefixes[0];
$suffix = count($suffixes) > 1 ? format_plural($item['value'], $suffixes[0], $suffixes[1]) : $suffixes[0];
$output = $prefix . $output . $suffix;
}
$element[$delta] = array(
'#markup' => $output,
);
}
break;
case 'number_unformatted':
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#markup' => $item['value'],
);
}
break;
}
return $element;
}
/**
* Implements hook_field_widget_info().
*/
function bigint_field_widget_info() {
return array(
'number_bigint' => array(
'label' => t('BigInt field'),
'field types' => array(
'number_bigint',
),
),
);
}
/**
* Implements hook_field_widget_form().
*/
function bigint_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$value = isset($items[$delta]['value']) ? $items[$delta]['value'] : '';
$element += array(
'#type' => 'textfield',
'#default_value' => $value,
'#size' => $instance['settings']['size'],
'#number_type' => str_replace('number_', '', $field['type']),
);
// Add prefix and suffix.
if (!empty($instance['settings']['prefix'])) {
$prefixes = explode('|', $instance['settings']['prefix']);
$element['#field_prefix'] = field_filter_xss(array_pop($prefixes));
}
if (!empty($instance['settings']['suffix'])) {
$suffixes = explode('|', $instance['settings']['suffix']);
$element['#field_suffix'] = field_filter_xss(array_pop($suffixes));
}
$element['#element_validate'][] = 'bigint_field_widget_validate';
return array(
'value' => $element,
);
}
/**
* FAPI validation of an individual number element.
*/
function bigint_field_widget_validate($element, &$form_state) {
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
$type = $element['#number_type'];
$value = $element['#value'];
// Reject invalid characters.
if (!empty($value)) {
switch ($type) {
case 'bigint':
$regexp = '@([^-0-9])|(.-)@';
$message = t('Only numbers are allowed in %field.', array(
'%field' => $instance['label'],
));
break;
}
if ($value != preg_replace($regexp, '', $value)) {
form_error($element, $message);
}
else {
form_set_value($element, $value, $form_state);
}
}
}
/**
* Implements hook_field_widget_error().
*/
function bigint_field_widget_error($element, $error, $form, &$form_state) {
form_error($element['value'], $error['message']);
}