You are here

function text_widget in Content Construction Kit (CCK) 5

Same name and namespace in other branches
  1. 6.3 modules/text/text.module \text_widget()
  2. 6 examples/simple_field.php \text_widget()
  3. 6 examples/example_field.php \text_widget()
  4. 6 modules/text/text.module \text_widget()
  5. 6.2 modules/text/text.module \text_widget()

Implementation of hook_widget().

File

./text.module, line 291
Defines simple text field types.

Code

function text_widget($op, &$node, $field, &$items) {
  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']]['#description'] = content_filter_xss(t($field['widget']['description']));
        $delta = 0;
        foreach ($items as $data) {
          if ($data['value']) {
            if ($field['widget']['rows'] == 1) {
              $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,
                '#maxlength' => $field['max_length'] ? $field['max_length'] : NULL,
                '#weight' => $field['widget']['weight'],
              );
            }
            else {
              $form[$field['field_name']][$delta]['value'] = array(
                '#type' => 'textarea',
                '#title' => $delta == 0 ? t($field['widget']['label']) : '',
                '#default_value' => $data['value'],
                '#required' => $delta == 0 ? $field['required'] : FALSE,
                '#rows' => $field['widget']['rows'],
                '#weight' => $field['widget']['weight'],
              );
            }
            if ($field['text_processing']) {
              $form[$field['field_name']][$delta]['format'] = filter_form($data['format'], $form[$field['field_name']][$delta]['value']['#weight'] + 1, array(
                $field['field_name'],
                $delta,
                'format',
              ));
            }
            $delta++;
          }
        }
        foreach (range($delta, $delta + 2) as $delta) {
          if ($field['widget']['rows'] == 1) {
            $form[$field['field_name']][$delta]['value'] = array(
              '#type' => 'textfield',
              '#title' => $delta == 0 ? t($field['widget']['label']) : '',
              '#default_value' => '',
              '#required' => $delta == 0 ? $field['required'] : FALSE,
              '#maxlength' => $field['max_length'] ? $field['max_length'] : NULL,
              '#weight' => $field['widget']['weight'],
            );
          }
          else {
            $form[$field['field_name']][$delta]['value'] = array(
              '#type' => 'textarea',
              '#title' => $delta == 0 ? t($field['widget']['label']) : '',
              '#default_value' => '',
              '#required' => $delta == 0 ? $field['required'] : FALSE,
              '#rows' => $field['widget']['rows'],
              '#weight' => $field['widget']['weight'],
            );
          }
          if ($field['text_processing']) {
            $form[$field['field_name']][$delta]['format'] = filter_form($items[$delta]['format'], $form[$field['field_name']][$delta]['value']['#weight'] + 1, array(
              $field['field_name'],
              $delta,
              'format',
            ));
          }
        }
      }
      else {
        if ($field['widget']['rows'] == 1) {
          $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'])),
            '#maxlength' => $field['max_length'] ? $field['max_length'] : NULL,
            '#weight' => $field['widget']['weight'],
          );
        }
        else {
          $form[$field['field_name']][0]['value'] = array(
            '#type' => 'textarea',
            '#title' => t($field['widget']['label']),
            '#default_value' => $items[0]['value'],
            '#required' => $field['required'],
            '#rows' => $field['widget']['rows'],
            '#description' => content_filter_xss(t($field['widget']['description'])),
            '#weight' => $field['widget']['weight'],
          );
        }
        if ($field['text_processing']) {
          $form[$field['field_name']][0]['format'] = filter_form($items[0]['format'], $form[$field['field_name']][0]['value']['#weight'] + 1, array(
            $field['field_name'],
            0,
            'format',
          ));
        }
      }
      return $form;
    case 'process form values':

      // Don't save empty fields except the first value
      foreach ($items as $delta => $item) {
        if ($item['value'] == '' && $delta > 0) {
          unset($items[$delta]);
        }
      }
      break;
  }
}