2009-02-05 03:42:58 +00:00
<?php
/**
* @file
* Defines numeric field types.
*/
2013-01-07 11:22:28 +00:00
use Drupal\Core\Entity\EntityInterface;
2009-12-15 05:20:13 +00:00
/**
* Implements hook_help().
*/
function number_help($path, $arg) {
switch ($path) {
case 'admin/help#number':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Number module defines various numeric field types for the Field module. Numbers can be in integer, decimal, or floating-point form, and they can be formatted when displayed. 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;
}
}
2009-02-05 03:42:58 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_info().
2009-02-05 03:42:58 +00:00
*/
function number_field_info() {
return array(
'number_integer' => array(
'label' => t('Integer'),
'description' => t('This field stores a number in the database as an integer.'),
'instance_settings' => array('min' => '', 'max' => '', 'prefix' => '', 'suffix' => ''),
'default_widget' => 'number',
2009-12-11 16:49:40 +00:00
'default_formatter' => 'number_integer',
2013-02-01 21:52:18 +00:00
'field item class' => '\Drupal\number\Type\IntegerItem',
2009-02-05 03:42:58 +00:00
),
'number_decimal' => array(
'label' => t('Decimal'),
'description' => t('This field stores a number in the database in a fixed decimal format.'),
2012-04-10 21:46:55 +00:00
'settings' => array('precision' => 10, 'scale' => 2),
2009-02-05 03:42:58 +00:00
'instance_settings' => array('min' => '', 'max' => '', 'prefix' => '', 'suffix' => ''),
'default_widget' => 'number',
2009-09-27 11:12:15 +00:00
'default_formatter' => 'number_decimal',
2013-02-01 21:52:18 +00:00
'field item class' => '\Drupal\number\Type\DecimalItem',
2009-02-05 03:42:58 +00:00
),
'number_float' => array(
'label' => t('Float'),
'description' => t('This field stores a number in the database in a floating point format.'),
'instance_settings' => array('min' => '', 'max' => '', 'prefix' => '', 'suffix' => ''),
'default_widget' => 'number',
2009-09-27 11:12:15 +00:00
'default_formatter' => 'number_decimal',
2013-02-01 21:52:18 +00:00
'field item class' => '\Drupal\number\Type\FloatItem',
2009-02-05 03:42:58 +00:00
),
);
}
2009-08-19 13:31:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_settings_form().
2009-08-19 13:31:14 +00:00
*/
2009-09-26 15:57:39 +00:00
function number_field_settings_form($field, $instance, $has_data) {
2009-08-19 13:31:14 +00:00
$settings = $field['settings'];
$form = array();
if ($field['type'] == 'number_decimal') {
$form['precision'] = array(
'#type' => 'select',
'#title' => t('Precision'),
'#options' => drupal_map_assoc(range(10, 32)),
'#default_value' => $settings['precision'],
'#description' => t('The total number of digits to store in the database, including those to the right of the decimal.'),
2009-09-26 15:57:39 +00:00
'#disabled' => $has_data,
2009-08-19 13:31:14 +00:00
);
$form['scale'] = array(
'#type' => 'select',
'#title' => t('Scale'),
'#options' => drupal_map_assoc(range(0, 10)),
'#default_value' => $settings['scale'],
'#description' => t('The number of digits to the right of the decimal.'),
2009-09-26 15:57:39 +00:00
'#disabled' => $has_data,
2009-08-19 13:31:14 +00:00
);
2009-11-10 06:48:58 +00:00
}
2009-08-19 13:31:14 +00:00
return $form;
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_instance_settings_form().
2009-08-19 13:31:14 +00:00
*/
function number_field_instance_settings_form($field, $instance) {
$settings = $instance['settings'];
$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.'),
2012-04-07 02:35:00 +00:00
'#element_validate' => array('form_validate_number'),
2009-08-19 13:31:14 +00:00
);
$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.'),
2012-04-07 02:35:00 +00:00
'#element_validate' => array('form_validate_number'),
2009-08-19 13:31:14 +00:00
);
$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,
2010-09-07 17:56:41 +00:00
'#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')."),
2009-08-19 13:31:14 +00:00
);
return $form;
}
2009-02-05 03:42:58 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_validate().
2009-03-26 13:31:28 +00:00
*
* Possible error codes:
2012-09-27 15:44:17 +00:00
* - number_min: The value is less than the allowed minimum value.
* - number_max: The value is greater than the allowed maximum value.
2009-02-05 03:42:58 +00:00
*/
2013-01-07 11:22:28 +00:00
function number_field_validate(EntityInterface $entity = NULL, $field, $instance, $langcode, $items, &$errors) {
2009-03-26 13:31:28 +00:00
foreach ($items as $delta => $item) {
if ($item['value'] != '') {
if (is_numeric($instance['settings']['min']) && $item['value'] < $instance['settings']['min']) {
2009-08-22 00:58:55 +00:00
$errors[$field['field_name']][$langcode][$delta][] = array(
2009-03-26 13:31:28 +00:00
'error' => 'number_min',
2011-05-16 00:39:12 +00:00
'message' => t('%name: the value may be no less than %min.', array('%name' => $instance['label'], '%min' => $instance['settings']['min'])),
2009-03-26 13:31:28 +00:00
);
}
if (is_numeric($instance['settings']['max']) && $item['value'] > $instance['settings']['max']) {
2009-08-22 00:58:55 +00:00
$errors[$field['field_name']][$langcode][$delta][] = array(
2009-03-26 13:31:28 +00:00
'error' => 'number_max',
2011-05-16 00:39:12 +00:00
'message' => t('%name: the value may be no greater than %max.', array('%name' => $instance['label'], '%max' => $instance['settings']['max'])),
2009-03-26 13:31:28 +00:00
);
2009-02-05 03:42:58 +00:00
}
}
}
}
2009-11-12 21:03:36 +00:00
/**
* Implements hook_field_presave().
*/
2013-01-07 11:22:28 +00:00
function number_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) {
2009-11-12 21:03:36 +00:00
if ($field['type'] == 'number_decimal') {
// Let PHP round the value to ensure consistent behavior across storage
// backends.
foreach ($items as $delta => $item) {
if (isset($item['value'])) {
$items[$delta]['value'] = round($item['value'], $field['settings']['scale']);
}
}
}
}
2009-02-05 03:42:58 +00:00
/**
2010-01-26 21:44:10 +00:00
* Implements hook_field_is_empty().
2009-02-05 03:42:58 +00:00
*/
function number_field_is_empty($item, $field) {
2010-05-06 05:59:31 +00:00
if (empty($item['value']) && (string) $item['value'] !== '0') {
2009-02-05 03:42:58 +00:00
return TRUE;
}
return FALSE;
}