CCK: Defines numeric field types. Note: Requires content.module.'); } } /** * Implementation of hook_field_info(). */ function number_field_info() { return array( 'number_integer' => array('label' => 'Integer'), 'number_decimal' => array('label' => 'Decimal'), ); } /** * Implementation of hook_field_settings(). */ function number_field_settings($op, $field) { switch ($op) { case 'form': $form = array(); $form['min'] = array( '#type' => 'textfield', '#title' => t('Minimum'), '#default_value' => isset($field['min']) ? $field['min'] : '', ); $form['max'] = array( '#type' => 'textfield', '#title' => t('Maximum'), '#default_value' => isset($field['max']) ? $field['max'] : '', ); $form['allowed_values'] = array( '#type' => 'textarea', '#title' => t('Allowed values'), '#default_value' => isset($field['allowed_values']) ? $field['allowed_values'] : '', '#required' => FALSE, '#rows' => 10, '#description' => t('The possible values this field can contain. Any other values will result in an error. Enter one value per line.'), ); return $form; case 'validate': if ($field['min'] && !is_numeric($field['min'])) { form_set_error('rows', t('"Minimum" must be a number.')); } if ($field['max'] && !is_numeric($field['max'])) { form_set_error('rows', t('"Maximum" must be a number.')); } break; case 'save': return array('min', 'max', 'allowed_values'); case 'database columns': if ($field['type'] == 'number_integer') { return array( 'value' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'sortable' => TRUE), ); } if ($field['type'] == 'number_decimal') { return array( 'value' => array('type' => 'float', 'not null' => TRUE, 'default' => 0, 'sortable' => TRUE), ); } case 'filters': $allowed_values = explode("\n", $field['allowed_values']); $allowed_values = array_map('trim', $allowed_values); $allowed_values = array_filter($allowed_values, 'strlen'); if (count($allowed_values)) { return array( 'default' => array( 'list' => drupal_map_assoc($allowed_values), 'list-type' => 'list', 'operator' => 'views_handler_operator_or', 'value-type' => 'array', ), ); } else { return array( 'default' => array( 'operator' => 'views_handler_operator_gtlt', ), ); } break; } } /** * Implementation of hook_field(). */ function number_field($op, &$node, $field, &$items, $teaser, $page) { switch ($op) { case 'view': foreach ($items as $delta => $item) { $items[$delta]['view'] = content_format($field, $item, 'default', $node); } return theme('field', $node, $field, $items, $teaser, $page); case 'validate': $allowed_values = explode("\n", $field['allowed_values']); $allowed_values = array_map('trim', $allowed_values); $allowed_values = array_filter($allowed_values, 'strlen'); if (is_array($items)) { foreach ($items as $delta => $item) { $error_field = $field['field_name'].']['.$delta.'][value'; if ($item['value'] != '') { if (is_numeric($field['min']) && $item['value'] < $field['min']) { form_set_error($error_field, t('The value of %name may be no smaller than %min.', array('%name' => t($field['widget']['label']), '%min' => $field['min']))); } if (is_numeric($field['max']) && $item['value'] > $field['max']) { form_set_error($error_field, t('The value of %name may be no larger than %max.', array('%name' => t($field['widget']['label']), '%max' => $field['max']))); } if (count($allowed_values) && !in_array($item['value'], $allowed_values)) { form_set_error($error_field, t('Illegal value for %name.', array('%name' => t($field['widget']['label'])))); } } } } break; } } /** * Implementation of hook_field_formatter_info(). */ function number_field_formatter_info() { return array( 'default' => array( 'label' => 'Default', 'field types' => array('number_integer', 'number_decimal'), ), ); } /** * Implementation of hook_field_formatter(). */ function number_field_formatter($field, $item, $formatter, $node) { return check_plain($item['value']); } /** * Implementation of hook_widget_info(). */ function number_widget_info() { return array( 'number' => array( 'label' => 'Text Field', 'field types' => array('number_integer', 'number_decimal'), ), ); } /** * Implementation of hook_widget(). */ function number_widget($op, &$node, $field, &$node_field) { switch ($op) { case 'form': $form = array(); $form[$field['field_name']] = array('#tree' => TRUE); if ($field['multiple']) { $form[$field['field_name']]['#type'] = 'fieldset'; $form[$field['field_name']]['#title'] = t($field['widget']['label']); $delta = 0; foreach ($node_field as $data) { if ($data['value']) { $form[$field['field_name']][$delta]['value'] = array( '#type' => 'textfield', '#title' => '', '#default_value' => $data['value'], '#required' => ($delta == 0) ? $field['required'] : FALSE, '#maxlength' => 11, ); $delta++; } } foreach (range($delta, $delta + 2) as $delta) { $form[$field['field_name']][$delta]['value'] = array( '#type' => 'textfield', '#title' => '', '#default_value' => '', '#required' => ($delta == 0) ? $field['required'] : FALSE, '#maxlength' => 11, ); } } else { $form[$field['field_name']][0]['value'] = array( '#type' => 'textfield', '#title' => t($field['widget']['label']), '#default_value' => isset($node_field[0]['value']) ? $node_field[0]['value'] : '', '#required' => $field['required'], '#description' => $field['widget']['description'], '#maxlength' => 11, ); } return $form; case 'process form values': // Don't save empty fields. if ($field['multiple']) { foreach ($node_field as $delta => $item) { if ($item['value'] == '') { unset($node_field[$delta]); } } } break; } }