text.module 25.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php
// $Id$

/**
 * @file
 * Defines simple text field types.
 */

/**
10
 * Implement hook_theme().
11
12
13
14
15
16
 */
function text_theme() {
  return array(
    'text_textarea' => array(
      'arguments' => array('element' => NULL),
    ),
17
18
19
    'text_textarea_with_summary' => array(
      'arguments' => array('element' => NULL),
    ),
20
21
22
23
24
25
26
27
28
29
30
31
    'text_textfield' => array(
      'arguments' => array('element' => NULL),
    ),
    'field_formatter_text_default' => array(
      'arguments' => array('element' => NULL),
    ),
    'field_formatter_text_plain' => array(
      'arguments' => array('element' => NULL),
    ),
    'field_formatter_text_trimmed' => array(
      'arguments' => array('element' => NULL),
    ),
32
33
34
    'field_formatter_text_summary_or_trimmed' => array(
      'arguments' => array('element' => NULL),
    ),
35
36
37
38
  );
}

/**
39
 * Implement hook_field_info().
40
41
42
43
44
45
46
47
 *
 * Field settings:
 *   - max_length: the maximum length for a varchar field.
 * Instance settings:
 *   - text_processing: whether text input filters should be used.
 *   - display_summary: whether the summary field should be displayed.
 *     When empty and not displayed the summary will take its value from the
 *     trimmed value of the main text field.
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 */
function text_field_info() {
  return array(
    'text' => array(
      'label' => t('Text'),
      'description' => t('This field stores varchar text in the database.'),
      'settings' => array('max_length' => 255),
      'instance_settings' => array('text_processing' => 0),
      'default_widget' => 'text_textfield',
      'default_formatter' => 'text_default',
    ),
    'text_long' => array(
      'label' => t('Long text'),
      'description' => t('This field stores long text in the database.'),
62
      'settings' => array('max_length' => ''),
63
64
65
66
      'instance_settings' => array('text_processing' => 0),
      'default_widget' => 'text_textarea',
      'default_formatter' => 'text_default',
    ),
67
68
69
70
71
72
73
74
    'text_with_summary' => array(
      'label' => t('Long text and summary'),
      'description' => t('This field stores long text in the database along with optional summary text.'),
      'settings' => array('max_length' => ''),
      'instance_settings' => array('text_processing' => 1, 'display_summary' => 0),
      'default_widget' => 'text_textarea_with_summary',
      'default_formatter' => 'text_summary_or_trimmed',
    ),
75
76
77
78
  );
}

/**
79
 * Implement hook_field_schema().
80
 */
81
function text_field_schema($field) {
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  switch ($field['type']) {
    case 'text':
      $columns = array(
        'value' => array(
          'type' => 'varchar',
          'length' => $field['settings']['max_length'],
          'not null' => FALSE,
        ),
      );
      break;
    case 'text_long':
      $columns = array(
        'value' => array(
          'type' => 'text',
          'size' => 'big',
          'not null' => FALSE,
        ),
      );
      break;
    case 'text_with_summary':
      $columns = array(
        'value' => array(
          'type' => 'text',
          'size' => 'big',
          'not null' => FALSE,
        ),
        'summary' => array(
          'type' => 'text',
          'size' => 'big',
          'not null' => FALSE,
        ),
      );
      break;
115
116
117
118
119
120
121
122
  }
  $columns += array(
    'format' => array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => FALSE,
    ),
  );
123
124
125
126
127
128
  return array(
    'columns' => $columns,
    'indexes' => array(
      'format' => array('format'),
    ),
  );
129
130
131
}

/**
132
 * Implement hook_field_validate().
133
134
 *
 * Possible error codes:
135
136
 * - 'text_value_max_length': The value exceeds the maximum length.
 * - 'text_summary_max_length': The summary exceeds the maximum length.
137
 */
138
139
function text_field_validate($obj_type, $object, $field, $instance, $items, &$errors) {
  foreach ($items as $delta => $item) {
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    foreach (array('value' => t('full text'), 'summary' => t('summary')) as $column => $desc) {
      if (!empty($item[$column])) {
        if (!empty($field['settings']['max_length']) && drupal_strlen($item[$column]) > $field['settings']['max_length']) {
          switch ($column) {
            case 'value':
              $message = t('%name: the text may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length']));
              break;
            case 'summary':
              $message = t('%name: the summary may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length']));
              break;
          }
          $errors[$field['field_name']][$delta][] = array(
            'error' => "text_{$column}_length",
            'message' => $message,
          );
        }
156
157
158
159
160
      }
    }
  }
}

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 * Implement hook_field_load().
 *
 * Where possible, generate the sanitized version of each field early so that
 * it is cached in the field cache. This avoids looking up from the filter cache
 * separately.
 * @see text_field_sanitize().
 */
function text_field_load($obj_type, $objects, $field, $instances, &$items) {
  global $language;

  foreach ($objects as $id => $object) {
    foreach ($items[$id] as $delta => $item) {
      if (!empty($instances[$id]['settings']['text_processing'])) {
        // Only process items with a cacheable format, the rest will be
        // handled by text_field_sanitize().
177
178
179
        $format = $item['format'];
        if (filter_format_allowcache($format)) {
          $lang = isset($object->language) ? $object->language : $language->language;
180
          $items[$id][$delta]['safe'] = isset($item['value']) ? check_markup($item['value'], $format, $lang, FALSE, FALSE) : '';
181
          if ($field['type'] == 'text_with_summary') {
182
            $items[$id][$delta]['safe_summary'] = isset($item['summary']) ? check_markup($item['summary'], $format, $lang, FALSE, FALSE) : '';
183
          }
184
185
186
        }
      }
      else {
187
188
189
190
        $items[$id][$delta]['safe'] = check_plain($item['value']);
        if ($field['type'] == 'text_with_summary') {
          $items[$id][$delta]['safe_summary'] = check_plain($item['summary']);
        }
191
192
193
194
195
196
197
198
199
200
      }
    }
  }
}

/**
 * Implement hook_field_sanitize().
 *
 * @see text_field_load()
 */
201
202
203
function text_field_sanitize($obj_type, $object, $field, $instance, &$items) {
  global $language;
  foreach ($items as $delta => $item) {
204
    // Only sanitize items which were not already processed inside
205
206
    // text_field_load(), i.e. items with uncacheable text formats, or coming
    // from a form preview.
207
208
    if (!isset($items[$delta]['safe'])) {
      if (!empty($instance['settings']['text_processing'])) {
209
210
        $format = $item['format'];
        $lang = isset($object->language) ? $object->language : $language->language;
211
        $items[$delta]['safe'] = isset($item['value']) ? check_markup($item['value'], $format, $lang, FALSE) : '';
212
        if ($field['type'] == 'text_with_summary') {
213
          $items[$delta]['safe_summary'] = isset($item['summary']) ? check_markup($item['summary'], $format, $lang, FALSE) : '';
214
        }
215
216
      }
      else {
217
218
219
220
        $item[$delta]['safe'] = check_plain($item['value']);
        if ($field['type'] == 'text_with_summary') {
          $item[$delta]['safe_summary'] = check_plain($item['summary']);
        }
221
      }
222
223
224
225
226
    }
  }
}

/**
227
 * Implement hook_field_is_empty().
228
229
230
231
232
233
234
235
236
 */
function text_field_is_empty($item, $field) {
  if (empty($item['value']) && (string)$item['value'] !== '0') {
    return TRUE;
  }
  return FALSE;
}

/**
237
 * Implement hook_field_formatter_info().
238
239
240
241
242
 */
function text_field_formatter_info() {
  return array(
    'text_default' => array(
      'label' => t('Default'),
243
      'field types' => array('text', 'text_long', 'text_with_summary'),
244
245
246
247
248
249
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
    'text_plain' => array(
      'label' => t('Plain text'),
250
      'field types' => array('text', 'text_long', 'text_with_summary'),
251
252
253
254
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
255
256
257
258
259
260

    // The text_trimmed formatter displays the trimmed version of the
    // full element of the field.  It is intended to be used with text
    // and text_long fields.  It also works with text_with_summary
    // fields though the text_summary_or_trimmed formatter makes more
    // sense for that field type.
261
262
    'text_trimmed' => array(
      'label' => t('Trimmed'),
263
264
265
266
267
268
269
270
271
272
273
274
275
      'field types' => array('text', 'text_long', 'text_with_summary'),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),

    // The 'summary or trimmed' field formatter for text_with_summary
    // fields displays returns the summary element of the field or, if
    // the summary is empty, the trimmed version of the full element
    // of the field.
    'text_summary_or_trimmed' => array(
      'label' => t('Summary or trimmed'),
      'field types' => array('text_with_summary'),
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}

/**
 * Theme function for 'default' text field formatter.
 */
function theme_field_formatter_text_default($element) {
  return $element['#item']['safe'];
}

/**
 * Theme function for 'plain' text field formatter.
 */
function theme_field_formatter_text_plain($element) {
  return strip_tags($element['#item']['safe']);
}

/**
 * Theme function for 'trimmed' text field formatter.
 */
function theme_field_formatter_text_trimmed($element) {
  $field = field_info_field($element['#field_name']);
  $instance = field_info_instance($element['#field_name'], $element['#bundle']);
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  return text_summary($element['#item']['safe'], $instance['settings']['text_processing'] ? $element['#item']['format'] : NULL);
}

/**
 * Theme function for 'summary or trimmed' field formatter for
 * text_with_summary fields.  This formatter returns the summary
 * element of the field or, if the summary is empty, the trimmed
 * version of the full element of the field.
 */
function theme_field_formatter_text_summary_or_trimmed($element) {
  $field = field_info_field($element['#field_name']);
  $instance = field_info_instance($element['#field_name'], $element['#bundle']);

  if (!empty($element['#item']['safe_summary'])) {
    return $element['#item']['safe_summary'];
  }
  else {
320
321
    $size = variable_get('teaser_length_' . $element['#bundle'], 600);
    return text_summary($element['#item']['safe'], $instance['settings']['text_processing'] ? $element['#item']['format'] : NULL, $size);
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
  }
}

/**
 * Generate a trimmed, formatted version of a text field value.
 *
 * If the end of the summary is not indicated using the <!--break--> delimiter
 * then we generate the summary automatically, trying to end it at a sensible
 * place such as the end of a paragraph, a line break, or the end of a
 * sentence (in that order of preference).
 *
 * @param $text
 *   The content for which a summary will be generated.
 * @param $format
 *   The format of the content.
 *   If the PHP filter is present and $text contains PHP code, we do not
 *   split it up to prevent parse errors.
 *   If the line break filter is present then we treat newlines embedded in
 *   $text as line breaks.
 *   If the htmlcorrector filter is present, it will be run on the generated
 *   summary (if different from the incoming $text).
 * @param $size
 *   The desired character length of the summary. If omitted, the default
 *   value will be used. Ignored if the special delimiter is present
 *   in $text.
 * @return
 *   The generated summary.
 */
function text_summary($text, $format = NULL, $size = NULL) {

  if (!isset($size)) {
    // What used to be called 'teaser' is now called 'summary', but
    // the variable 'teaser_length' is preserved for backwards compatibility.
    $size = variable_get('teaser_length', 600);
  }

  // Find where the delimiter is in the body
  $delimiter = strpos($text, '<!--break-->');

  // If the size is zero, and there is no delimiter, the entire body is the summary.
  if ($size == 0 && $delimiter === FALSE) {
    return $text;
  }

  // If a valid delimiter has been specified, use it to chop off the summary.
  if ($delimiter !== FALSE) {
    return substr($text, 0, $delimiter);
  }

  // We check for the presence of the PHP evaluator filter in the current
  // format. If the body contains PHP code, we do not split it up to prevent
  // parse errors.
  if (isset($format)) {
    $filters = filter_list_format($format);
    if (isset($filters['php/0']) && strpos($text, '<?') !== FALSE) {
      return $text;
    }
  }

  // If we have a short body, the entire body is the summary.
  if (drupal_strlen($text) <= $size) {
    return $text;
  }

  // If the delimiter has not been specified, try to split at paragraph or
  // sentence boundaries.

  // The summary may not be longer than maximum length specified. Initial slice.
  $summary = truncate_utf8($text, $size);

  // Store the actual length of the UTF8 string -- which might not be the same
  // as $size.
  $max_rpos = strlen($summary);

  // How much to cut off the end of the summary so that it doesn't end in the
  // middle of a paragraph, sentence, or word.
  // Initialize it to maximum in order to find the minimum.
  $min_rpos = $max_rpos;

  // Store the reverse of the summary. We use strpos on the reversed needle and
  // haystack for speed and convenience.
  $reversed = strrev($summary);

  // Build an array of arrays of break points grouped by preference.
  $break_points = array();

  // A paragraph near the end of sliced summary is most preferable.
  $break_points[] = array('</p>' => 0);

  // If no complete paragraph then treat line breaks as paragraphs.
  $line_breaks = array('<br />' => 6, '<br>' => 4);
  // Newline only indicates a line break if line break converter
  // filter is present.
  if (isset($filters['filter/1'])) {
    $line_breaks["\n"] = 1;
  }
  $break_points[] = $line_breaks;

  // If the first paragraph is too long, split at the end of a sentence.
  $break_points[] = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1);

  // Iterate over the groups of break points until a break point is found.
  foreach ($break_points as $points) {
    // Look for each break point, starting at the end of the summary.
    foreach ($points as $point => $offset) {
      // The summary is already reversed, but the break point isn't.
      $rpos = strpos($reversed, strrev($point));
      if ($rpos !== FALSE) {
        $min_rpos = min($rpos + $offset, $min_rpos);
      }
    }

    // If a break point was found in this group, slice and stop searching.
    if ($min_rpos !== $max_rpos) {
      // Don't slice with length 0. Length must be <0 to slice from RHS.
      $summary = ($min_rpos === 0) ? $summary : substr($summary, 0, 0 - $min_rpos);
      break;
    }
  }

  // If the htmlcorrector filter is present, apply it to the generated summary.
  if (isset($filters['filter/3'])) {
    $summary = _filter_htmlcorrector($summary);
  }

  return $summary;
448
449
450
}

/**
451
 * Implement hook_field_widget_info().
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
 *
 * Here we indicate that the field module will handle
 * the default value and multiple values for these widgets.
 *
 * Callbacks can be omitted if default handing is used.
 * They're included here just so this module can be used
 * as an example for custom modules that might do things
 * differently.
 */
function text_field_widget_info() {
  return array(
    'text_textfield' => array(
      'label' => t('Text field'),
      'field types' => array('text'),
      'settings' => array('size' => 60),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
    'text_textarea' => array(
      'label' => t('Text area (multiple rows)'),
      'field types' => array('text_long'),
      'settings' => array('rows' => 5),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
481
482
483
484
485
486
487
488
489
    'text_textarea_with_summary' => array(
      'label' => t('Text area with a summary'),
      'field types' => array('text_with_summary'),
      'settings' => array('rows' => 20, 'summary_rows' => 5),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
490
491
492
493
  );
}

/**
494
 * Implement FAPI hook_elements().
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
 *
 * Any FAPI callbacks needed for individual widgets can be declared here,
 * and the element will be passed to those callbacks for processing.
 *
 * Drupal will automatically theme the element using a theme with
 * the same name as the hook_elements key.
 *
 * Autocomplete_path is not used by text_field_widget but other
 * widgets can use it (see nodereference and userreference).
 */
function text_elements() {
  return array(
    'text_textfield' => array(
      '#input' => TRUE,
      '#columns' => array('value'), '#delta' => 0,
510
      '#process' => array('text_textfield_elements_process'),
511
      '#theme_wrapper' => 'text_textfield',
512
      '#autocomplete_path' => FALSE,
513
    ),
514
515
516
    'text_textarea' => array(
      '#input' => TRUE,
      '#columns' => array('value', 'format'), '#delta' => 0,
517
      '#process' => array('text_textarea_elements_process'),
518
      '#theme_wrapper' => 'text_textarea',
519
      '#filter_value' => FILTER_FORMAT_DEFAULT,
520
521
522
523
524
525
526
527
    ),
    'text_textarea_with_summary' => array(
      '#input' => TRUE,
      '#columns' => array('value', 'format', 'summary'), '#delta' => 0,
      '#process' => array('text_textarea_with_summary_process'),
      '#theme_wrapper' => 'text_textarea_with_summary',
      '#filter_value' => FILTER_FORMAT_DEFAULT,
    ),
528
529
530
531
  );
}

/**
532
 * Implement hook_field_widget().
533
534
535
536
537
538
539
540
 *
 * Attach a single form element to the form. It will be built out and
 * validated in the callback(s) listed in hook_elements. We build it
 * out in the callbacks rather than here in hook_field_widget so it can be
 * plugged into any module that can provide it with valid
 * $field information.
 *
 * Field module will set the weight, field name and delta values
541
 * for each form element.
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
 *
 * If there are multiple values for this field, the field module will
 * call this function as many times as needed.
 *
 * @param $form
 *   the entire form array, $form['#node'] holds node information
 * @param $form_state
 *   the form_state, $form_state['values'][$field['field_name']]
 *   holds the field's form values.
 * @param $field
 *   The field structure.
 * @param $instance
 *   the field instance array
 * @param $items
 *   array of default values for this field
 * @param $delta
 *   the order of this item in the array of subelements (0, 1, 2, etc)
 *
 * @return
 *   the form item for a single element for this field
 */
function text_field_widget(&$form, &$form_state, $field, $instance, $items, $delta = 0) {
  $element = array(
    '#type' => $instance['widget']['type'],
    '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
  );
568
569
570
571
  if (!empty($instance['settings']['text_processing'])) {
    $element['#value_callback'] = 'text_field_widget_formatted_text_value';
  }

572
573
574
  return $element;
}

575
/**
576
 * Implement hook_field_widget_error().
577
578
 */
function text_field_widget_error($element, $error) {
579
580
581
582
583
584
585
586
587
588
589
  switch ($error['error']) {
    case 'text_summary_max_length':
      $error_element = $element[$element['#columns'][1]];
      break;

    default:
      $error_element = $element[$element['#columns'][0]];
      break;
  }

  form_error($error_element, $error['message']);
590
591
}

592
593
594
595
596
597
598
599
600
601
602
603
604
/**
 * Process an individual element.
 *
 * Build the form element. When creating a form using FAPI #process,
 * note that $element['#value'] is already set.
 *
 * The $field and $instance arrays are in $form['#fields'][$element['#field_name']].
 *
 * TODO: For widgets to be actual FAPI 'elements', reusable outside of a
 * 'field' context, they shoudn't rely on $field and $instance. The bits of
 * information needed to adjust the behavior of the 'element' should be
 * extracted in hook_field_widget() above.
 */
605
function text_textfield_elements_process($element, $form_state, $form) {
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  $field = $form['#fields'][$element['#field_name']]['field'];
  $instance = $form['#fields'][$element['#field_name']]['instance'];
  $field_key = $element['#columns'][0];
  $delta = $element['#delta'];

  $element[$field_key] = array(
    '#type' => 'textfield',
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
    '#autocomplete_path' => $element['#autocomplete_path'],
    '#size' => $instance['widget']['settings']['size'],
    '#attributes' => array('class' => 'text'),
    '#title' => $element['#title'],
    '#description' => $element['#description'],
    '#required' => $element['#required'],
  );

  $element[$field_key]['#maxlength'] = !empty($field['settings']['max_length']) ? $field['settings']['max_length'] : NULL;

  if (!empty($instance['settings']['text_processing'])) {
625
    $filter_key  = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
626
    $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
627
    $element[$field_key]['#text_format'] = $format;
628
629
630
631
632
633
634
635
636
637
638
639
640
  }

  return $element;
}

/**
 * Process an individual element.
 *
 * Build the form element. When creating a form using FAPI #process,
 * note that $element['#value'] is already set.
 *
 * The $field and $instance arrays are in $form['#fields'][$element['#field_name']].
 */
641
function text_textarea_elements_process($element, $form_state, $form) {
642
643
644
645
  $field = $form['#fields'][$element['#field_name']]['field'];
  $instance = $form['#fields'][$element['#field_name']]['instance'];
  $field_key = $element['#columns'][0];
  $delta = $element['#delta'];
646

647
648
649
650
651
652
653
654
655
656
657
  $element[$field_key] = array(
    '#type' => 'textarea',
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
    '#rows' => $instance['widget']['settings']['rows'],
    '#weight' => 0,
    '#title' => $element['#title'],
    '#description' => $element['#description'],
    '#required' => $element['#required'],
  );

  if (!empty($instance['settings']['text_processing'])) {
658
    $filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
659
    $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
    $element[$field_key]['#text_format'] = $format;
  }

  return $element;
}

/**
 * Process an individual element.
 *
 * Build the form element. When creating a form using FAPI #process,
 * note that $element['#value'] is already set.
 *
 * The $field and $instance arrays are in $form['#fields'][$element['#field_name']].
 */
function text_textarea_with_summary_process($element, $form_state, $form) {
  $field = $form['#fields'][$element['#field_name']]['field'];
  $instance = $form['#fields'][$element['#field_name']]['instance'];
  $delta = $element['#delta'];

  $field_key = $element['#columns'][1];
  $display = !empty($element['#value'][$field_key]) || !empty($instance['settings']['display_summary']);
  $element[$field_key] = array(
    '#title' => t('Summary'),
    '#type' => $display ? 'textarea' : 'value',
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
    '#rows' => $instance['widget']['settings']['summary_rows'],
    '#weight' => 0,
    '#title' => t('Summary'),
    '#description' => t('Leave blank to use trimmed value of full text as the summary.'),
    '#required' => $element['#required'],
    '#display' => $display,
  );

  $field_key = $element['#columns'][0];
  $element[$field_key] = array(
    '#type' => 'textarea',
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
    '#rows' => $instance['widget']['settings']['rows'],
    '#weight' => 1,
    '#title' => $display ? t('Full text') : $element['#title'],
    '#description' => $element['#description'],
    '#required' => $element['#required'],
    '#required' => $instance['required'],
  );

  if (!empty($instance['settings']['text_processing'])) {
    $filter_key  = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
    $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
    $element[$field_key]['#text_format'] = $format;
709
  }
710
711

  return $element;
712
713
}

714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/**
 * Helper function to determine the value for a formatted text widget.
 *
 * '#text_format' puts the format in '[column 0]_format' in incoming values,
 * while we need it in '[column 1]'.
 */
function text_field_widget_formatted_text_value($form, $edit = FALSE) {
  if ($edit !== FALSE) {
    $field_key = $form['#columns'][0];
    $filter_key = (count($form['#columns']) == 2) ? $form['#columns'][1] : 'format';
    $default_key = $field_key . '_format';
    // The format selector uses #access = FALSE if only one format is
    // available. In this case, we don't receive its value, and need to
    // manually set it.
    $edit['format'] = !empty($edit[$default_key]) ? $edit[$default_key] : filter_resolve_format(FILTER_FORMAT_DEFAULT);
    unset($edit[$default_key]);
    return $edit;
  }
}

734
735
736
737
738
739
740
741
742
/**
 * FAPI theme for an individual text elements.
 *
 * The textfield or textarea is already rendered by the
 * textfield or textarea themes and the html output
 * lives in $element['#children']. Override this theme to
 * make custom changes to the output.
 *
 * $element['#field_name'] contains the field name
743
 * $element['#delta] is the position of this element in the group
744
745
746
747
748
749
750
 */
function theme_text_textfield($element) {
  return $element['#children'];
}

function theme_text_textarea($element) {
  return $element['#children'];
751
}
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769

function theme_text_textarea_with_summary($element) {
  // If displaying both a textarea and a summary field, wrap them
  // in a fieldset to make it clear they belong together.
  $field_key = $element['#columns'][1];
  if (!empty($element[$field_key]['#display'])) {
    $fieldset = array(
      '#title' => $element['#title'],
      '#value' => $element['#children'],
      '#attributes' => array('class' => 'text-textarea'),
      '#id' => str_replace('_', '-', $element['#field_name']) . '-summary-wrapper',
    );
    return theme('fieldset', $fieldset);
  }
  else {
    return $element['#children'];
  }
}
770