text.module 12.2 KB
Newer Older
1 2 3 4 5
<?php
// $Id$

/**
 * @file
6
 * Defines simple text field types.
7 8 9 10 11 12 13 14
 */

/**
 * Implementation of hook_help().
 */
function text_help($section) {
  switch ($section) {
    case 'admin/modules#description':
15
      return t('<strong>CCK:</strong> Defines simple text field types. <em>Note: Requires content.module.</em>');
16 17 18 19 20 21 22 23
  }
}

/**
 * Implementation of hook_field_info().
 */
function text_field_info() {
  return array(
24
    'text' => array('label' => 'Text'),
25 26 27 28 29 30
  );
}

/**
 * Implementation of hook_field_settings().
 */
31
function text_field_settings($op, $field) {
32 33 34
  switch ($op) {
    case 'form':
      $form = array();
35 36 37 38
      $options = array(0 => t('Plain text'), 1 => t('Filtered text (user selects input format)'));
      $form['text_processing'] = array(
        '#type' => 'radios',
        '#title' => t('Text processing'),
39
        '#default_value' => isset($field['text_processing']) ? $field['text_processing'] : 0,
40 41
        '#options' => $options,
      );
42 43 44
      $form['max_length'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum length'),
45
        '#default_value' => isset($field['max_length']) ? $field['max_length'] : '',
46 47 48
        '#required' => FALSE,
        '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
      );
49 50
      $form['allowed_values'] = array(
        '#type' => 'textarea',
51
        '#title' => t('Allowed values list'),
52
        '#default_value' => isset($field['allowed_values']) ? $field['allowed_values'] : '',
53 54
        '#required' => FALSE,
        '#rows' => 10,
55 56 57 58 59 60 61 62 63 64 65 66 67 68
        '#description' => t('The possible values this field can contain. Enter one value per line, in the format key|label. The key is the value that will be stored in the database and it must match the field storage type, %type. The label is optional and the key will be used as the label if no label is specified.', array('%type' => $field['type'])),
      );
      $form['advanced_options'] = array(
        '#type' => 'fieldset',
        '#title' => t('Php code'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['advanced_options']['allowed_values_php'] = array(
        '#type' => 'textarea',
        '#title' => t('Code'),
        '#default_value' => $field['allowed_values_php'],
        '#rows' => 6,
        '#description' => t('Advanced Usage Only: PHP code that returns a keyed array of allowed values. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the array returned by this code will override the allowed values list above.'),
69
      );
70
      return $form;
71

72
    case 'save':
73
      return array('text_processing', 'max_length', 'allowed_values', 'allowed_values_php');
74 75 76

    case 'database columns':
      $columns = array(
JonBob's avatar
JonBob committed
77
        'value' => array('type' => 'varchar', 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
78 79
        'format' => array('type' => 'int', 'length' => 10, 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      );
JonBob's avatar
JonBob committed
80
      if ($field['max_length'] == 0 || $field['max_length'] > 255) {
81 82 83 84 85
        $columns['value']['type'] = 'longtext';
      }
      else {
        $columns['value']['length'] = $field['max_length'];
      }
86 87 88 89
      if ($field['text_processing'] == 0) {
        unset($columns['format']);
      }
      return $columns;
90

91
    case 'filters':
92 93 94 95 96 97 98 99 100 101 102
      $allowed_values = text_allowed_values($field);
       if (count($allowed_values)) {
         return array(
           'default' => array(
            'list' => $allowed_values,
             'list-type' => 'list',
             'operator' => 'views_handler_operator_or',
             'value-type' => 'array',
            ),
          );
        }
103 104 105 106 107 108 109 110 111
      else {
        return array(
          'like' => array(
            'operator' => 'views_handler_operator_like',
            'handler' => 'views_handler_filter_like',
          ),
        );
      }
      break;
112 113 114 115 116 117
  }
}

/**
 * Implementation of hook_field().
 */
118
function text_field($op, &$node, $field, &$items, $teaser, $page) {
119
  switch ($op) {
120
    case 'validate':
121
      $allowed_values = text_allowed_values($field);
122

123 124
      if (is_array($items)) {
        foreach ($items as $delta => $item) {
125
          $error_field = $field['field_name'].']['.$delta.'][value';
126
          if ($item['value'] != '') {
127
            if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) {
128
              form_set_error($error_field, t('Illegal value for %name.', array('%name' => t($field['widget']['label']))));
129 130 131 132
            }
          }
        }
      }
133 134 135 136 137 138 139 140
      if ($field['max_length'] > 0) {
        foreach ($items as $delta => $data) {
          $error_field = $field['field_name'].']['.$delta.'][value';
          if (strlen($data['value']) > $field['max_length']) {
            form_set_error($error_field, t('%label is longer than %max characters.', array('%label' => $field['widget']['label'], '%max' => $field['max_length'])));
          }
        }
      }
141
      break;
142 143 144
  }
}

145
/**
146
 * Implementation of hook_field_formatter_info().
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
 */
function text_field_formatter_info() {
  return array(
    'default' => array(
      'label' => 'Default',
      'field types' => array('text'),
    ),
    'plain' => array(
      'label' => 'Plain text',
      'field types' => array('text'),
    ),
    'trimmed' => array(
      'label' => 'Trimmed',
      'field types' => array('text'),
    ),
  );
}

/**
 * Implementation of hook_field_formatter().
167
 *
168
 * The $node argument is necessary so that filter access can be checked on
169 170
 * node preview.
 */
171 172
function text_field_formatter($field, $item, $formatter, $node) {
  if (!isset($item['value'])) {
173 174
    return '';
  }
175

176 177 178
  if ($allowed_values = text_allowed_values($field)) {
    return $allowed_values[$item['value']];
  }
179

180
  if ($field['text_processing']) {
181
    $text = check_markup($item['value'], $item['format'], is_null($node) || isset($node->in_preview));
182 183
  }
  else {
184 185
    $text = check_plain($item['value']);
  }
186

187 188 189
  switch ($formatter) {
    case 'plain':
      return strip_tags($text);
190

191 192
    case 'trimmed':
      return node_teaser($text, $field['text_processing'] ? $item['format'] : NULL);
193

194 195
    default:
      return $text;
196 197 198
  }
}

199

200 201 202 203 204 205 206
/**
 * Implementation of hook_widget_info().
 */
function text_widget_info() {
  return array(
    'text' => array(
      'label' => 'Text Field',
207
      'field types' => array('text'),
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    ),
  );
}

/**
 * Implementation of hook_widget_settings().
 */
function text_widget_settings($op, $widget) {
  switch ($op) {
    case 'form':
      $form = array();
      $form['rows'] = array(
        '#type' => 'textfield',
        '#title' => t('Rows'),
        '#default_value' => $widget['rows'] ? $widget['rows'] : 1,
        '#required' => TRUE,
      );
      return $form;

    case 'validate':
      if (!is_numeric($widget['rows']) || intval($widget['rows']) != $widget['rows'] || $widget['rows'] <= 0) {
        form_set_error('rows', t('"Rows" must be a positive integer.'));
      }
      break;

    case 'save':
      return array('rows');
  }
}


/**
 * Implementation of hook_widget().
 */
242
function text_widget($op, &$node, $field, &$items) {
243 244 245 246 247 248 249
  switch ($op) {
    case 'form':
      $form = array();

      $form[$field['field_name']] = array('#tree' => TRUE);

      if ($field['multiple']) {
250
        $form[$field['field_name']]['#type'] = 'fieldset';
251
        $form[$field['field_name']]['#description'] = $field['widget']['description'];
252
        $delta = 0;
253
        foreach ($items as $data) {
254 255 256 257
          if ($data['value']) {
            if ($field['widget']['rows'] == 1) {
              $form[$field['field_name']][$delta]['value'] = array(
                '#type' => 'textfield',
258
                '#title' => ($delta == 0) ? t($field['widget']['label']) : '',
259 260
                '#default_value' => $data['value'],
                '#required' => ($delta == 0) ? $field['required'] : FALSE,
261
                '#maxlength' => $field['max_length'] ? $field['max_length'] : '',
262
                '#weight' => $field['widget']['weight'],
263 264 265 266 267
              );
            }
            else {
              $form[$field['field_name']][$delta]['value'] = array(
                '#type' => 'textarea',
268
                '#title' => ($delta == 0) ? t($field['widget']['label']) : '',
269 270 271
                '#default_value' => $data['value'],
                '#required' => ($delta == 0) ? $field['required'] : FALSE,
                '#rows' => $field['widget']['rows'],
272
                '#weight' => $field['widget']['weight'],
273 274 275
              );
            }
            if ($field['text_processing']) {
276
              $form[$field['field_name']][$delta]['format'] = filter_form($data['format'], $form[$field['field_name']][$delta]['value']['#weight'] + 1, array($field['field_name'], $delta, 'format'));
277 278 279 280 281
            }
            $delta++;
          }
        }
        foreach (range($delta, $delta + 2) as $delta) {
JonBob's avatar
JonBob committed
282
          if ($field['widget']['rows'] == 1) {
283 284
            $form[$field['field_name']][$delta]['value'] = array(
              '#type' => 'textfield',
285
              '#title' => ($delta == 0) ? t($field['widget']['label']) : '',
286
              '#default_value' => '',
287
              '#required' => ($delta == 0) ? $field['required'] : FALSE,
288
              '#maxlength' => $field['max_length'] ? $field['max_length'] : '',
289
              '#weight' => $field['widget']['weight'],
290 291 292 293 294
            );
          }
          else {
            $form[$field['field_name']][$delta]['value'] = array(
              '#type' => 'textarea',
295
              '#title' => ($delta == 0) ? t($field['widget']['label']) : '',
296
              '#default_value' => '',
297 298
              '#required' => ($delta == 0) ? $field['required'] : FALSE,
              '#rows' => $field['widget']['rows'],
299
              '#weight' => $field['widget']['weight'],
300 301 302
            );
          }
          if ($field['text_processing']) {
303
            $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'));
304 305 306 307
          }
        }
      }
      else {
JonBob's avatar
JonBob committed
308
        if ($field['widget']['rows'] == 1) {
309
          $form[$field['field_name']][0]['value'] = array(
310 311
            '#type' => 'textfield',
            '#title' => t($field['widget']['label']),
312
            '#default_value' => isset($items[0]['value']) ? $items[0]['value'] : '',
313
            '#required' => $field['required'],
JonBob's avatar
JonBob committed
314
            '#description' => $field['widget']['description'],
315
            '#maxlength' => $field['max_length'] ? $field['max_length'] : '',
316
            '#weight' => $field['widget']['weight'],
317 318 319
          );
        }
        else {
320
          $form[$field['field_name']][0]['value'] = array(
321 322
            '#type' => 'textarea',
            '#title' => t($field['widget']['label']),
323
            '#default_value' => $items[0]['value'],
324 325
            '#required' => $field['required'],
            '#rows' => $field['widget']['rows'],
JonBob's avatar
JonBob committed
326
            '#description' => $field['widget']['description'],
327
            '#weight' => $field['widget']['weight'],
328 329 330
          );
        }
        if ($field['text_processing']) {
331
          $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'));
332 333 334
        }
      }
      return $form;
335 336

    case 'process form values':
337 338 339 340
      // Don't save empty fields except the first value
      foreach ($items as $delta => $item) {
        if ($item['value'] == '' && $delta > 0) {
          unset($items[$delta]);
341 342 343
        }
      }
      break;
344 345
  }
}
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

/**
 *  Create an array of the allowed values for this field
 */
function text_allowed_values($field) {
  static $allowed_values;

  if ($allowed_values[$field['field_name']]) {
    return $allowed_values[$field['field_name']];
  }

  $allowed_values[$field['field_name']] = array();
  if ($field['allowed_values_php']) {
    ob_start();
    $result = eval($field['allowed_values_php']);
    if (is_array($result)) {
      $allowed_values[$field['field_name']] = $result;
    }
    ob_end_clean();
  }
  if (!$allowed_values[$field['field_name']]) {

    $list = explode("\n", $field['allowed_values']);
    $list = array_map('trim', $list);
    $list = array_filter($list, 'strlen');
    foreach ($list as $opt) {
372
      list($key, $value) = explode('|', $opt);
373 374 375 376 377
      $allowed_values[$field['field_name']][$key] = $value ? $value : $key;
    }
  }
  return $allowed_values[$field['field_name']];
}