~spreadubuntu/spreadubuntu/devel-drupal6

« back to all changes in this revision

Viewing changes to modules/cck/modules/text/text.module

  • Committer: ruben
  • Date: 2009-06-08 09:38:49 UTC
  • Revision ID: ruben@captive-20090608093849-s1qtsyctv2vwp1x1
SpreadUbuntu moving to Drupal6. Based on ubuntu-drupal theme and adding our modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id: text.module,v 1.95.2.28 2008/12/30 00:00:54 yched Exp $
 
3
 
 
4
/**
 
5
 * @file
 
6
 * Defines simple text field types.
 
7
 */
 
8
 
 
9
/**
 
10
 * Implementation of hook_theme().
 
11
 */
 
12
function text_theme() {
 
13
  return array(
 
14
    'text_textarea' => array(
 
15
      'arguments' => array('element' => NULL),
 
16
    ),
 
17
    'text_textfield' => array(
 
18
      'arguments' => array('element' => NULL),
 
19
    ),
 
20
    'text_formatter_default' => array(
 
21
      'arguments' => array('element' => NULL),
 
22
    ),
 
23
    'text_formatter_plain' => array(
 
24
      'arguments' => array('element' => NULL),
 
25
    ),
 
26
    'text_formatter_trimmed' => array(
 
27
      'arguments' => array('element' => NULL),
 
28
    ),
 
29
    'text_formatter_foo' => array(
 
30
      'arguments' => array('element' => NULL),
 
31
    ),
 
32
  );
 
33
}
 
34
 
 
35
/**
 
36
 * Implementation of hook_field_info().
 
37
 */
 
38
function text_field_info() {
 
39
  return array(
 
40
    'text' => array(
 
41
      'label' => t('Text'),
 
42
      'description' => t('Store text in the database.'),
 
43
    ),
 
44
  );
 
45
}
 
46
 
 
47
/**
 
48
 * Implementation of hook_field_settings().
 
49
 */
 
50
function text_field_settings($op, $field) {
 
51
  switch ($op) {
 
52
    case 'form':
 
53
      $form = array();
 
54
      $options = array(0 => t('Plain text'), 1 => t('Filtered text (user selects input format)'));
 
55
      $form['text_processing'] = array(
 
56
        '#type' => 'radios',
 
57
        '#title' => t('Text processing'),
 
58
        '#default_value' => is_numeric($field['text_processing']) ? $field['text_processing'] : 0,
 
59
        '#options' => $options,
 
60
      );
 
61
      $form['max_length'] = array(
 
62
        '#type' => 'textfield',
 
63
        '#title' => t('Maximum length'),
 
64
        '#default_value' => is_numeric($field['max_length']) ? $field['max_length'] : '',
 
65
        '#required' => FALSE,
 
66
        '#element_validate' => array('_element_validate_integer_positive'),
 
67
        '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
 
68
      );
 
69
      $form['allowed_values_fieldset'] = array(
 
70
        '#type' => 'fieldset',
 
71
        '#title' => t('Allowed values'),
 
72
        '#collapsible' => TRUE,
 
73
        '#collapsed' => TRUE,
 
74
      );
 
75
      $form['allowed_values_fieldset']['allowed_values'] = array(
 
76
        '#type' => 'textarea',
 
77
        '#title' => t('Allowed values list'),
 
78
        '#default_value' => !empty($field['allowed_values']) ? $field['allowed_values'] : '',
 
79
        '#required' => FALSE,
 
80
        '#rows' => 10,
 
81
        '#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.<br />Allowed HTML tags: @tags', array('%type' => $field['type'], '@tags' => _content_filter_xss_display_allowed_tags())),
 
82
      );
 
83
      $form['allowed_values_fieldset']['advanced_options'] = array(
 
84
        '#type' => 'fieldset',
 
85
        '#title' => t('PHP code'),
 
86
        '#collapsible' => TRUE,
 
87
        '#collapsed' => empty($field['allowed_values_php']),
 
88
      );
 
89
      if (user_access('Use PHP input for field settings (dangerous - grant with care)')) {
 
90
        $form['allowed_values_fieldset']['advanced_options']['allowed_values_php'] = array(
 
91
          '#type' => 'textarea',
 
92
          '#title' => t('Code'),
 
93
          '#default_value' => !empty($field['allowed_values_php']) ? $field['allowed_values_php'] : '',
 
94
          '#rows' => 6,
 
95
          '#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.'),
 
96
        );
 
97
      }
 
98
      else {
 
99
        $form['allowed_values_fieldset']['advanced_options']['markup_allowed_values_php'] = array(
 
100
          '#type' => 'item',
 
101
          '#title' => t('Code'),
 
102
          '#value' => !empty($field['allowed_values_php']) ? '<code>'. check_plain($field['allowed_values_php']) .'</code>' : t('&lt;none&gt;'),
 
103
          '#description' => empty($field['allowed_values_php']) ? t("You're not allowed to input PHP code.") : t('This PHP code was set by an administrator and will override the allowed values list above.'),
 
104
        );
 
105
      }
 
106
      return $form;
 
107
 
 
108
    case 'save':
 
109
      return array('text_processing', 'max_length', 'allowed_values', 'allowed_values_php');
 
110
 
 
111
    case 'database columns':
 
112
      if (empty($field['max_length']) || $field['max_length'] > 255) {
 
113
        $columns['value'] = array('type' => 'text', 'size' => 'big', 'not null' => FALSE, 'sortable' => TRUE, 'views' => TRUE);
 
114
      }
 
115
      else {
 
116
        $columns['value'] = array('type' => 'varchar', 'length' => $field['max_length'], 'not null' => FALSE, 'sortable' => TRUE, 'views' => TRUE);
 
117
      }
 
118
      if (!empty($field['text_processing'])) {
 
119
        $columns['format'] = array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'views' => FALSE);
 
120
      }
 
121
      return $columns;
 
122
 
 
123
    case 'views data':
 
124
      $allowed_values = content_allowed_values($field);
 
125
      if (count($allowed_values)) {
 
126
        $data = content_views_field_views_data($field);
 
127
        $db_info = content_database_info($field);
 
128
        $table_alias = content_views_tablename($field);
 
129
 
 
130
        // Filter: Add a 'many to one' filter.
 
131
        $copy = $data[$table_alias][$field['field_name'] .'_value'];
 
132
        $copy['title'] = t('@label (!name) - Allowed values', array('@label' => t($field['widget']['label']), '!name' => $field['field_name']));
 
133
        $copy['filter']['handler'] = 'content_handler_filter_many_to_one';
 
134
        unset($copy['field'], $copy['argument'], $copy['sort']);
 
135
        $data[$table_alias][$field['field_name'] .'_value_many_to_one'] = $copy;
 
136
        // Argument : swap the handler to the 'many to one' operator.
 
137
        $data[$table_alias][$field['field_name'] .'_value']['argument']['handler'] = 'content_handler_argument_many_to_one';
 
138
        return $data;
 
139
      }
 
140
  }
 
141
}
 
142
 
 
143
/**
 
144
 * Implementation of hook_field().
 
145
 */
 
146
function text_field($op, &$node, $field, &$items, $teaser, $page) {
 
147
  switch ($op) {
 
148
    case 'validate':
 
149
      $allowed_values = content_allowed_values($field);
 
150
      if (is_array($items)) {
 
151
        foreach ($items as $delta => $item) {
 
152
          $error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
 
153
          if (is_array($item) && isset($item['_error_element'])) unset($item['_error_element']);
 
154
          if (!empty($item['value'])) {
 
155
            if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) {
 
156
              form_set_error($error_element, t('%name: illegal value.', array('%name' => t($field['widget']['label']))));
 
157
            }
 
158
            if (!empty($field['max_length']) && drupal_strlen($item['value']) > $field['max_length']) {
 
159
              form_set_error($error_element, t('%name: the value may not be longer than %max characters.', array('%name' => $field['widget']['label'], '%max' => $field['max_length'])));
 
160
            }
 
161
          }
 
162
        }
 
163
      }
 
164
      return $items;
 
165
 
 
166
    case 'sanitize':
 
167
      foreach ($items as $delta => $item) {
 
168
        if (!empty($field['text_processing'])) {
 
169
          $check = is_null($node) || (isset($node->build_mode) && $node->build_mode == NODE_BUILD_PREVIEW);
 
170
          $text = isset($item['value']) ? check_markup($item['value'], $item['format'], $check) : '';
 
171
        }
 
172
        else {
 
173
          $text = check_plain($item['value']);
 
174
        }
 
175
        $items[$delta]['safe'] = $text;
 
176
      }
 
177
  }
 
178
}
 
179
 
 
180
/**
 
181
 * Implementation of hook_content_is_empty().
 
182
 */
 
183
function text_content_is_empty($item, $field) {
 
184
  if (empty($item['value']) && (string)$item['value'] !== '0') {
 
185
    return TRUE;
 
186
  }
 
187
  return FALSE;
 
188
}
 
189
 
 
190
/**
 
191
 * Implementation of hook_field_formatter_info().
 
192
 */
 
193
function text_field_formatter_info() {
 
194
  return array(
 
195
    'default' => array(
 
196
      'label' => t('Default'),
 
197
      'field types' => array('text'),
 
198
      'multiple values' => CONTENT_HANDLE_CORE,
 
199
    ),
 
200
    'plain' => array(
 
201
      'label' => t('Plain text'),
 
202
      'field types' => array('text'),
 
203
      'multiple values' => CONTENT_HANDLE_CORE,
 
204
    ),
 
205
    'trimmed' => array(
 
206
      'label' => t('Trimmed'),
 
207
      'field types' => array('text'),
 
208
      'multiple values' => CONTENT_HANDLE_CORE,
 
209
    ),
 
210
  );
 
211
}
 
212
 
 
213
/**
 
214
 * Theme function for 'default' text field formatter.
 
215
 */
 
216
function theme_text_formatter_default($element) {
 
217
  return ($allowed =_text_allowed_values($element)) ? $allowed : $element['#item']['safe'];
 
218
}
 
219
 
 
220
/**
 
221
 * Theme function for 'plain' text field formatter.
 
222
 */
 
223
function theme_text_formatter_plain($element) {
 
224
  return ($allowed =_text_allowed_values($element)) ? $allowed : strip_tags($element['#item']['safe']);
 
225
}
 
226
 
 
227
/**
 
228
 * Theme function for 'trimmed' text field formatter.
 
229
 */
 
230
function theme_text_formatter_trimmed($element) {
 
231
  $field = content_fields($element['#field_name'], $element['#type_name']);
 
232
  return ($allowed =_text_allowed_values($element)) ? $allowed : node_teaser($element['#item']['safe'], $field['text_processing'] ? $element['#item']['format'] : NULL);
 
233
}
 
234
 
 
235
function _text_allowed_values($element) {
 
236
  $field = content_fields($element['#field_name'], $element['#type_name']);
 
237
  if (($allowed_values = content_allowed_values($field)) && isset($allowed_values[$element['#item']['value']])) {
 
238
    return $allowed_values[$element['#item']['value']];
 
239
  }
 
240
}
 
241
 
 
242
/**
 
243
 * Implementation of hook_widget_info().
 
244
 *
 
245
 * Here we indicate that the content module will handle
 
246
 * the default value and multiple values for these widgets.
 
247
 *
 
248
 * Callbacks can be omitted if default handing is used.
 
249
 * They're included here just so this module can be used
 
250
 * as an example for custom modules that might do things
 
251
 * differently.
 
252
 */
 
253
function text_widget_info() {
 
254
  return array(
 
255
    'text_textfield' => array(
 
256
      'label' => t('Text field'),
 
257
      'field types' => array('text'),
 
258
      'multiple values' => CONTENT_HANDLE_CORE,
 
259
      'callbacks' => array(
 
260
        'default value' => CONTENT_CALLBACK_DEFAULT,
 
261
      ),
 
262
    ),
 
263
    'text_textarea' => array(
 
264
      'label' => t('Text area (multiple rows)'),
 
265
      'field types' => array('text'),
 
266
      'multiple values' => CONTENT_HANDLE_CORE,
 
267
      'callbacks' => array(
 
268
        'default value' => CONTENT_CALLBACK_DEFAULT,
 
269
      ),
 
270
    ),
 
271
  );
 
272
}
 
273
 
 
274
/**
 
275
 * Implementation of FAPI hook_elements().
 
276
 *
 
277
 * Any FAPI callbacks needed for individual widgets can be declared here,
 
278
 * and the element will be passed to those callbacks for processing.
 
279
 *
 
280
 * Drupal will automatically theme the element using a theme with
 
281
 * the same name as the hook_elements key.
 
282
 *
 
283
 * Autocomplete_path is not used by text_widget but other widgets can use it
 
284
 * (see nodereference and userreference).
 
285
 */
 
286
function text_elements() {
 
287
  return array(
 
288
    'text_textfield' => array(
 
289
      '#input' => TRUE,
 
290
      '#columns' => array('value'), '#delta' => 0,
 
291
      '#process' => array('text_textfield_process'),
 
292
      '#autocomplete_path' => FALSE,
 
293
    ),
 
294
    'text_textarea' => array(
 
295
      '#input' => TRUE,
 
296
      '#columns' => array('value', 'format'), '#delta' => 0,
 
297
      '#process' => array('text_textarea_process'),
 
298
      '#filter_value' => FILTER_FORMAT_DEFAULT,
 
299
    ),
 
300
  );
 
301
}
 
302
 
 
303
/**
 
304
 * Implementation of hook_widget_settings().
 
305
 */
 
306
function text_widget_settings($op, $widget) {
 
307
  switch ($op) {
 
308
    case 'form':
 
309
      $form = array();
 
310
      $rows = (isset($widget['rows']) && is_numeric($widget['rows'])) ? $widget['rows'] : 5;
 
311
      $size = (isset($widget['size']) && is_numeric($widget['size'])) ? $widget['size'] : 60;
 
312
      if ($widget['type'] == 'text_textfield') {
 
313
        $form['rows'] = array('#type' => 'hidden', '#value' => $rows);
 
314
        $form['size'] = array(
 
315
          '#type' => 'textfield',
 
316
          '#title' => t('Size of textfield'),
 
317
          '#default_value' => $size,
 
318
          '#element_validate' => array('_element_validate_integer_positive'),
 
319
          '#required' => TRUE,
 
320
        );
 
321
      }
 
322
      else {
 
323
        $form['rows'] = array(
 
324
          '#type' => 'textfield',
 
325
          '#title' => t('Rows'),
 
326
          '#default_value' => $rows,
 
327
          '#element_validate' => array('_element_validate_integer_positive'),
 
328
          '#required' => TRUE,
 
329
        );
 
330
        $form['size'] = array('#type' => 'hidden', '#value' => $size);
 
331
      }
 
332
      return $form;
 
333
 
 
334
    case 'save':
 
335
      return array('rows', 'size');
 
336
  }
 
337
}
 
338
 
 
339
/**
 
340
 * Implementation of hook_widget().
 
341
 *
 
342
 * Attach a single form element to the form. It will be built out and
 
343
 * validated in the callback(s) listed in hook_elements. We build it
 
344
 * out in the callbacks rather than here in hook_widget so it can be
 
345
 * plugged into any module that can provide it with valid
 
346
 * $field information.
 
347
 *
 
348
 * Content module will set the weight, field name and delta values
 
349
 * for each form element. This is a change from earlier CCK versions
 
350
 * where the widget managed its own multiple values.
 
351
 *
 
352
 * If there are multiple values for this field, the content module will
 
353
 * call this function as many times as needed.
 
354
 *
 
355
 * @param $form
 
356
 *   the entire form array, $form['#node'] holds node information
 
357
 * @param $form_state
 
358
 *   the form_state, $form_state['values'][$field['field_name']]
 
359
 *   holds the field's form values.
 
360
 * @param $field
 
361
 *   the field array
 
362
 * @param $items
 
363
 *   array of default values for this field
 
364
 * @param $delta
 
365
 *   the order of this item in the array of subelements (0, 1, 2, etc)
 
366
 *
 
367
 * @return
 
368
 *   the form item for a single element for this field
 
369
 */
 
370
function text_widget(&$form, &$form_state, $field, $items, $delta = 0) {
 
371
  $element = array(
 
372
    '#type' => $field['widget']['type'],
 
373
    '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
 
374
  );
 
375
  return $element;
 
376
}
 
377
 
 
378
/**
 
379
 * Process an individual element.
 
380
 *
 
381
 * Build the form element. When creating a form using FAPI #process,
 
382
 * note that $element['#value'] is already set.
 
383
 *
 
384
 * The $fields array is in $form['#field_info'][$element['#field_name']].
 
385
 */
 
386
function text_textfield_process($element, $edit, $form_state, $form) {
 
387
  $field = $form['#field_info'][$element['#field_name']];
 
388
  $field_key = $element['#columns'][0];
 
389
  $delta = $element['#delta'];
 
390
  $element[$field_key] = array(
 
391
    '#type' => 'textfield',
 
392
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
 
393
    '#autocomplete_path' => $element['#autocomplete_path'],
 
394
    '#size' => !empty($field['widget']['size']) ? $field['widget']['size'] : 60,
 
395
    '#attributes' => array('class' => 'text'),
 
396
    // The following values were set by the content module and need
 
397
    // to be passed down to the nested element.
 
398
    '#title' => $element['#title'],
 
399
    '#description' => $element['#description'],
 
400
    '#required' => $element['#required'],
 
401
    '#field_name' => $element['#field_name'],
 
402
    '#type_name' => $element['#type_name'],
 
403
    '#delta' => $element['#delta'],
 
404
    '#columns' => $element['#columns'],
 
405
  );
 
406
 
 
407
  $element[$field_key]['#maxlength'] = !empty($field['max_length']) ? $field['max_length'] : NULL;
 
408
 
 
409
  if (!empty($field['text_processing'])) {
 
410
    $filter_key = $element['#columns'][1];
 
411
    $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
 
412
    $parents = array_merge($element['#parents'] , array($filter_key));
 
413
    $element[$filter_key] = filter_form($format, 1, $parents);
 
414
  }
 
415
 
 
416
  // Used so that hook_field('validate') knows where to flag an error.
 
417
  $element['_error_element'] = array(
 
418
    '#type' => 'value',
 
419
    '#value' => implode('][', array_merge($element['#parents'], array($field_key))),
 
420
  );
 
421
 
 
422
  return $element;
 
423
}
 
424
 
 
425
/**
 
426
 * Process an individual element.
 
427
 *
 
428
 * Build the form element. When creating a form using FAPI #process,
 
429
 * note that $element['#value'] is already set.
 
430
 *
 
431
 * The $fields array is in $form['#field_info'][$element['#field_name']].
 
432
 */
 
433
function text_textarea_process($element, $edit, $form_state, $form) {
 
434
  $field = $form['#field_info'][$element['#field_name']];
 
435
  $field_key   = $element['#columns'][0];
 
436
  $element[$field_key] = array(
 
437
    '#type' => 'textarea',
 
438
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
 
439
    '#rows' => !empty($field['widget']['rows']) ? $field['widget']['rows'] : 10,
 
440
    '#weight' => 0,
 
441
    // The following values were set by the content module and need
 
442
    // to be passed down to the nested element.
 
443
    '#title' => $element['#title'],
 
444
    '#description' => $element['#description'],
 
445
    '#required' => $element['#required'],
 
446
    '#field_name' => $element['#field_name'],
 
447
    '#type_name' => $element['#type_name'],
 
448
    '#delta' => $element['#delta'],
 
449
    '#columns' => $element['#columns'],
 
450
  );
 
451
 
 
452
  if (!empty($field['text_processing'])) {
 
453
    $filter_key  = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
 
454
    $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
 
455
    $parents = array_merge($element['#parents'] , array($filter_key));
 
456
    $element[$filter_key] = filter_form($format, 1, $parents);
 
457
  }
 
458
 
 
459
  // Used so that hook_field('validate') knows where to flag an error.
 
460
  $element['_error_element'] = array(
 
461
    '#type' => 'value',
 
462
    '#value' => implode('][', array_merge($element['#parents'], array($field_key))),
 
463
  );
 
464
 
 
465
  return $element;
 
466
}
 
467
 
 
468
/**
 
469
 * FAPI theme for an individual text elements.
 
470
 *
 
471
 * The textfield or textarea is already rendered by the
 
472
 * textfield or textarea themes and the html output
 
473
 * lives in $element['#children']. Override this theme to
 
474
 * make custom changes to the output.
 
475
 *
 
476
 * $element['#field_name'] contains the field name
 
477
 * $element['#delta]  is the position of this element in the group
 
478
 */
 
479
function theme_text_textfield($element) {
 
480
  return $element['#children'];
 
481
}
 
482
 
 
483
function theme_text_textarea($element) {
 
484
  return $element['#children'];
 
485
}
 
 
b'\\ No newline at end of file'