You are here

function number_widget in Content Construction Kit (CCK) 5

Same name and namespace in other branches
  1. 6.3 modules/number/number.module \number_widget()
  2. 6 modules/number/number.module \number_widget()
  3. 6.2 modules/number/number.module \number_widget()

Implementation of hook_widget().

File

./number.module, line 273
Defines numeric field types.

Code

function number_widget($op, &$node, $field, &$items) {
  switch ($op) {
    case 'form':
      $form = array();
      $prefixes = explode('|', $field['prefix']);
      $suffixes = explode('|', $field['suffix']);

      // We take the plural form (if any) for forms.
      $prefix = content_filter_xss(array_pop($prefixes));
      $suffix = content_filter_xss(array_pop($suffixes));
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
      );
      if ($field['multiple']) {
        $form[$field['field_name']]['#type'] = 'fieldset';
        $form[$field['field_name']]['#description'] = content_filter_xss(t($field['widget']['description']));
        $delta = 0;
        foreach ($items as $data) {
          if (isset($data['value'])) {
            $form[$field['field_name']][$delta]['value'] = array(
              '#type' => 'textfield',
              '#title' => $delta == 0 ? t($field['widget']['label']) : '',
              '#default_value' => $data['value'],
              '#required' => $delta == 0 ? $field['required'] : FALSE,
              '#size' => 20,
              '#maxlength' => 11,
              '#attributes' => array(
                'class' => 'number',
              ),
              '#field_prefix' => $prefix,
              '#field_suffix' => $suffix,
            );
            $delta++;
          }
        }
        foreach (range($delta, $delta + 2) as $delta) {
          $form[$field['field_name']][$delta]['value'] = array(
            '#type' => 'textfield',
            '#title' => $delta == 0 ? t($field['widget']['label']) : '',
            '#default_value' => '',
            '#required' => $delta == 0 ? $field['required'] : FALSE,
            '#size' => 20,
            '#maxlength' => 11,
            '#attributes' => array(
              'class' => 'number',
            ),
            '#field_prefix' => $prefix,
            '#field_suffix' => $suffix,
          );
        }
      }
      else {
        $form[$field['field_name']][0]['value'] = array(
          '#type' => 'textfield',
          '#title' => t($field['widget']['label']),
          '#default_value' => isset($items[0]['value']) ? $items[0]['value'] : '',
          '#required' => $field['required'],
          '#description' => content_filter_xss(t($field['widget']['description'])),
          '#size' => 20,
          '#maxlength' => 11,
          '#attributes' => array(
            'class' => 'number',
          ),
          '#field_prefix' => $prefix,
          '#field_suffix' => $suffix,
        );
      }
      return $form;
    case 'process form values':

      // Don't save empty fields.
      foreach ($items as $delta => $item) {
        $items[$delta]['value'] = preg_replace('@[^-0-9\\.]@', '', $items[$delta]['value']);
      }
      if ($field['multiple']) {
        foreach ($items as $delta => $item) {
          if ($item['value'] == '' && $delta > 0) {
            unset($items[$delta]);
          }
        }
      }
      break;
  }
}