1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Defines simple text field types.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_help().
|
10 |
|
|
*/
|
11 |
|
|
function text_help($path, $arg) {
|
12 |
|
|
switch ($path) {
|
13 |
|
|
case 'admin/help#text':
|
14 |
|
|
$output = '';
|
15 |
|
|
$output .= '<h3>' . t('About') . '</h3>';
|
16 |
|
|
$output .= '<p>' . t("The Text module defines various text field types for the Field module. A text field may contain plain text only, or optionally, may use Drupal's <a href='@filter-help'>text filters</a> to securely manage HTML output. Text input fields may be either a single line (text field), multiple lines (text area), or for greater input control, a select box, checkbox, or radio buttons. If desired, the field can be validated, so that it is limited to a set of allowed values. See the <a href='@field-help'>Field module help page</a> for more information about fields.", array('@field-help' => url('admin/help/field'), '@filter-help' => url('admin/help/filter'))) . '</p>';
|
17 |
|
|
return $output;
|
18 |
|
|
}
|
19 |
|
|
}
|
20 |
|
|
|
21 |
|
|
/**
|
22 |
|
|
* Implements hook_field_info().
|
23 |
|
|
*
|
24 |
|
|
* Field settings:
|
25 |
|
|
* - max_length: the maximum length for a varchar field.
|
26 |
|
|
* Instance settings:
|
27 |
|
|
* - text_processing: whether text input filters should be used.
|
28 |
|
|
* - display_summary: whether the summary field should be displayed.
|
29 |
|
|
* When empty and not displayed the summary will take its value from the
|
30 |
|
|
* trimmed value of the main text field.
|
31 |
|
|
*/
|
32 |
|
|
function text_field_info() {
|
33 |
|
|
return array(
|
34 |
|
|
'text' => array(
|
35 |
|
|
'label' => t('Text'),
|
36 |
|
|
'description' => t('This field stores varchar text in the database.'),
|
37 |
|
|
'settings' => array('max_length' => 255),
|
38 |
|
|
'instance_settings' => array('text_processing' => 0),
|
39 |
|
|
'default_widget' => 'text_textfield',
|
40 |
|
|
'default_formatter' => 'text_default',
|
41 |
|
|
),
|
42 |
|
|
'text_long' => array(
|
43 |
|
|
'label' => t('Long text'),
|
44 |
|
|
'description' => t('This field stores long text in the database.'),
|
45 |
|
|
'instance_settings' => array('text_processing' => 0),
|
46 |
|
|
'default_widget' => 'text_textarea',
|
47 |
|
|
'default_formatter' => 'text_default',
|
48 |
|
|
),
|
49 |
|
|
'text_with_summary' => array(
|
50 |
|
|
'label' => t('Long text and summary'),
|
51 |
|
|
'description' => t('This field stores long text in the database along with optional summary text.'),
|
52 |
|
|
'instance_settings' => array('text_processing' => 1, 'display_summary' => 0),
|
53 |
|
|
'default_widget' => 'text_textarea_with_summary',
|
54 |
|
|
'default_formatter' => 'text_default',
|
55 |
|
|
),
|
56 |
|
|
);
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
/**
|
60 |
|
|
* Implements hook_field_settings_form().
|
61 |
|
|
*/
|
62 |
|
|
function text_field_settings_form($field, $instance, $has_data) {
|
63 |
|
|
$settings = $field['settings'];
|
64 |
|
|
|
65 |
|
|
$form = array();
|
66 |
|
|
|
67 |
|
|
if ($field['type'] == 'text') {
|
68 |
|
|
$form['max_length'] = array(
|
69 |
|
|
'#type' => 'textfield',
|
70 |
|
|
'#title' => t('Maximum length'),
|
71 |
|
|
'#default_value' => $settings['max_length'],
|
72 |
|
|
'#required' => TRUE,
|
73 |
|
|
'#description' => t('The maximum length of the field in characters.'),
|
74 |
|
|
'#element_validate' => array('element_validate_integer_positive'),
|
75 |
|
|
// @todo: If $has_data, add a validate handler that only allows
|
76 |
|
|
// max_length to increase.
|
77 |
|
|
'#disabled' => $has_data,
|
78 |
|
|
);
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
return $form;
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
/**
|
85 |
|
|
* Implements hook_field_instance_settings_form().
|
86 |
|
|
*/
|
87 |
|
|
function text_field_instance_settings_form($field, $instance) {
|
88 |
|
|
$settings = $instance['settings'];
|
89 |
|
|
|
90 |
|
|
$form['text_processing'] = array(
|
91 |
|
|
'#type' => 'radios',
|
92 |
|
|
'#title' => t('Text processing'),
|
93 |
|
|
'#default_value' => $settings['text_processing'],
|
94 |
|
|
'#options' => array(
|
95 |
|
|
t('Plain text'),
|
96 |
|
|
t('Filtered text (user selects text format)'),
|
97 |
|
|
),
|
98 |
|
|
);
|
99 |
|
|
if ($field['type'] == 'text_with_summary') {
|
100 |
|
|
$form['display_summary'] = array(
|
101 |
|
|
'#type' => 'checkbox',
|
102 |
|
|
'#title' => t('Summary input'),
|
103 |
|
|
'#default_value' => $settings['display_summary'],
|
104 |
|
|
'#description' => t('This allows authors to input an explicit summary, to be displayed instead of the automatically trimmed text when using the "Summary or trimmed" display type.'),
|
105 |
|
|
);
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
return $form;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
/**
|
112 |
|
|
* Implements hook_field_validate().
|
113 |
|
|
*
|
114 |
|
|
* Possible error codes:
|
115 |
|
|
* - 'text_value_max_length': The value exceeds the maximum length.
|
116 |
|
|
* - 'text_summary_max_length': The summary exceeds the maximum length.
|
117 |
|
|
*/
|
118 |
|
|
function text_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
|
119 |
|
|
foreach ($items as $delta => $item) {
|
120 |
|
|
// @todo Length is counted separately for summary and value, so the maximum
|
121 |
|
|
// length can be exceeded very easily.
|
122 |
|
|
foreach (array('value', 'summary') as $column) {
|
123 |
|
|
if (!empty($item[$column])) {
|
124 |
|
|
if (!empty($field['settings']['max_length']) && drupal_strlen($item[$column]) > $field['settings']['max_length']) {
|
125 |
|
|
switch ($column) {
|
126 |
|
|
case 'value':
|
127 |
|
|
$message = t('%name: the text may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length']));
|
128 |
|
|
break;
|
129 |
|
|
|
130 |
|
|
case 'summary':
|
131 |
|
|
$message = t('%name: the summary may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length']));
|
132 |
|
|
break;
|
133 |
|
|
}
|
134 |
|
|
$errors[$field['field_name']][$langcode][$delta][] = array(
|
135 |
|
|
'error' => "text_{$column}_length",
|
136 |
|
|
'message' => $message,
|
137 |
|
|
);
|
138 |
|
|
}
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/**
|
145 |
|
|
* Implements hook_field_load().
|
146 |
|
|
*
|
147 |
|
|
* Where possible, generate the sanitized version of each field early so that
|
148 |
|
|
* it is cached in the field cache. This avoids looking up from the filter cache
|
149 |
|
|
* separately.
|
150 |
|
|
*
|
151 |
|
|
* @see text_field_formatter_view()
|
152 |
|
|
*/
|
153 |
|
|
function text_field_load($entity_type, $entities, $field, $instances, $langcode, &$items) {
|
154 |
|
|
foreach ($entities as $id => $entity) {
|
155 |
|
|
foreach ($items[$id] as $delta => $item) {
|
156 |
|
|
// Only process items with a cacheable format, the rest will be handled
|
157 |
|
|
// by formatters if needed.
|
158 |
|
|
if (empty($instances[$id]['settings']['text_processing']) || filter_format_allowcache($item['format'])) {
|
159 |
|
|
$items[$id][$delta]['safe_value'] = isset($item['value']) ? _text_sanitize($instances[$id], $langcode, $item, 'value') : '';
|
160 |
|
|
if ($field['type'] == 'text_with_summary') {
|
161 |
|
|
$items[$id][$delta]['safe_summary'] = isset($item['summary']) ? _text_sanitize($instances[$id], $langcode, $item, 'summary') : '';
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
/**
|
169 |
|
|
* Implements hook_field_is_empty().
|
170 |
|
|
*/
|
171 |
|
|
function text_field_is_empty($item, $field) {
|
172 |
|
|
if (!isset($item['value']) || $item['value'] === '') {
|
173 |
|
|
return !isset($item['summary']) || $item['summary'] === '';
|
174 |
|
|
}
|
175 |
|
|
return FALSE;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
/**
|
179 |
|
|
* Implements hook_field_formatter_info().
|
180 |
|
|
*/
|
181 |
|
|
function text_field_formatter_info() {
|
182 |
|
|
return array(
|
183 |
|
|
'text_default' => array(
|
184 |
|
|
'label' => t('Default'),
|
185 |
|
|
'field types' => array('text', 'text_long', 'text_with_summary'),
|
186 |
|
|
),
|
187 |
|
|
'text_plain' => array(
|
188 |
|
|
'label' => t('Plain text'),
|
189 |
|
|
'field types' => array('text', 'text_long', 'text_with_summary'),
|
190 |
|
|
),
|
191 |
|
|
|
192 |
|
|
// The text_trimmed formatter displays the trimmed version of the
|
193 |
|
|
// full element of the field. It is intended to be used with text
|
194 |
|
|
// and text_long fields. It also works with text_with_summary
|
195 |
|
|
// fields though the text_summary_or_trimmed formatter makes more
|
196 |
|
|
// sense for that field type.
|
197 |
|
|
'text_trimmed' => array(
|
198 |
|
|
'label' => t('Trimmed'),
|
199 |
|
|
'field types' => array('text', 'text_long', 'text_with_summary'),
|
200 |
|
|
'settings' => array('trim_length' => 600),
|
201 |
|
|
),
|
202 |
|
|
|
203 |
|
|
// The 'summary or trimmed' field formatter for text_with_summary
|
204 |
|
|
// fields displays returns the summary element of the field or, if
|
205 |
|
|
// the summary is empty, the trimmed version of the full element
|
206 |
|
|
// of the field.
|
207 |
|
|
'text_summary_or_trimmed' => array(
|
208 |
|
|
'label' => t('Summary or trimmed'),
|
209 |
|
|
'field types' => array('text_with_summary'),
|
210 |
|
|
'settings' => array('trim_length' => 600),
|
211 |
|
|
),
|
212 |
|
|
);
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
/**
|
216 |
|
|
* Implements hook_field_formatter_settings_form().
|
217 |
|
|
*/
|
218 |
|
|
function text_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
|
219 |
|
|
$display = $instance['display'][$view_mode];
|
220 |
|
|
$settings = $display['settings'];
|
221 |
|
|
|
222 |
|
|
$element = array();
|
223 |
|
|
|
224 |
|
|
if (strpos($display['type'], '_trimmed') !== FALSE) {
|
225 |
|
|
$element['trim_length'] = array(
|
226 |
30d5b9c5
|
Mathieu Schiano
|
'#title' => t('Trimmed limit'),
|
227 |
85ad3d82
|
Assos Assos
|
'#type' => 'textfield',
|
228 |
30d5b9c5
|
Mathieu Schiano
|
'#field_suffix' => t('characters'),
|
229 |
85ad3d82
|
Assos Assos
|
'#size' => 10,
|
230 |
|
|
'#default_value' => $settings['trim_length'],
|
231 |
|
|
'#element_validate' => array('element_validate_integer_positive'),
|
232 |
30d5b9c5
|
Mathieu Schiano
|
'#description' => t('If the summary is not set, the trimmed %label field will be shorter than this character limit.', array('%label' => $instance['label'])),
|
233 |
85ad3d82
|
Assos Assos
|
'#required' => TRUE,
|
234 |
|
|
);
|
235 |
|
|
}
|
236 |
|
|
|
237 |
|
|
return $element;
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
/**
|
241 |
|
|
* Implements hook_field_formatter_settings_summary().
|
242 |
|
|
*/
|
243 |
|
|
function text_field_formatter_settings_summary($field, $instance, $view_mode) {
|
244 |
|
|
$display = $instance['display'][$view_mode];
|
245 |
|
|
$settings = $display['settings'];
|
246 |
|
|
|
247 |
|
|
$summary = '';
|
248 |
|
|
|
249 |
|
|
if (strpos($display['type'], '_trimmed') !== FALSE) {
|
250 |
30d5b9c5
|
Mathieu Schiano
|
$summary = t('Trimmed limit: @trim_length characters', array('@trim_length' => $settings['trim_length']));
|
251 |
85ad3d82
|
Assos Assos
|
}
|
252 |
|
|
|
253 |
|
|
return $summary;
|
254 |
|
|
}
|
255 |
|
|
|
256 |
|
|
/**
|
257 |
|
|
* Implements hook_field_formatter_view().
|
258 |
|
|
*/
|
259 |
|
|
function text_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
|
260 |
|
|
$element = array();
|
261 |
|
|
|
262 |
|
|
switch ($display['type']) {
|
263 |
|
|
case 'text_default':
|
264 |
|
|
case 'text_trimmed':
|
265 |
|
|
foreach ($items as $delta => $item) {
|
266 |
|
|
$output = _text_sanitize($instance, $langcode, $item, 'value');
|
267 |
|
|
if ($display['type'] == 'text_trimmed') {
|
268 |
|
|
$output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length']);
|
269 |
|
|
}
|
270 |
|
|
$element[$delta] = array('#markup' => $output);
|
271 |
|
|
}
|
272 |
|
|
break;
|
273 |
|
|
|
274 |
|
|
case 'text_summary_or_trimmed':
|
275 |
|
|
foreach ($items as $delta => $item) {
|
276 |
|
|
if (!empty($item['summary'])) {
|
277 |
|
|
$output = _text_sanitize($instance, $langcode, $item, 'summary');
|
278 |
|
|
}
|
279 |
|
|
else {
|
280 |
|
|
$output = _text_sanitize($instance, $langcode, $item, 'value');
|
281 |
|
|
$output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length']);
|
282 |
|
|
}
|
283 |
|
|
$element[$delta] = array('#markup' => $output);
|
284 |
|
|
}
|
285 |
|
|
break;
|
286 |
|
|
|
287 |
|
|
case 'text_plain':
|
288 |
|
|
foreach ($items as $delta => $item) {
|
289 |
|
|
$element[$delta] = array('#markup' => strip_tags($item['value']));
|
290 |
|
|
}
|
291 |
|
|
break;
|
292 |
|
|
}
|
293 |
|
|
|
294 |
|
|
return $element;
|
295 |
|
|
}
|
296 |
|
|
|
297 |
|
|
/**
|
298 |
|
|
* Sanitizes the 'value' or 'summary' data of a text value.
|
299 |
|
|
*
|
300 |
|
|
* Depending on whether the field instance uses text processing, data is run
|
301 |
|
|
* through check_plain() or check_markup().
|
302 |
|
|
*
|
303 |
|
|
* @param $instance
|
304 |
|
|
* The instance definition.
|
305 |
|
|
* @param $langcode
|
306 |
|
|
* The language associated to $item.
|
307 |
|
|
* @param $item
|
308 |
|
|
* The field value to sanitize.
|
309 |
|
|
* @param $column
|
310 |
|
|
* The column to sanitize (either 'value' or 'summary').
|
311 |
|
|
*
|
312 |
|
|
* @return
|
313 |
|
|
* The sanitized string.
|
314 |
|
|
*/
|
315 |
|
|
function _text_sanitize($instance, $langcode, $item, $column) {
|
316 |
|
|
// If the value uses a cacheable text format, text_field_load() precomputes
|
317 |
|
|
// the sanitized string.
|
318 |
|
|
if (isset($item["safe_$column"])) {
|
319 |
|
|
return $item["safe_$column"];
|
320 |
|
|
}
|
321 |
|
|
return $instance['settings']['text_processing'] ? check_markup($item[$column], $item['format'], $langcode) : check_plain($item[$column]);
|
322 |
|
|
}
|
323 |
|
|
|
324 |
|
|
/**
|
325 |
|
|
* Generate a trimmed, formatted version of a text field value.
|
326 |
|
|
*
|
327 |
|
|
* If the end of the summary is not indicated using the <!--break--> delimiter
|
328 |
|
|
* then we generate the summary automatically, trying to end it at a sensible
|
329 |
|
|
* place such as the end of a paragraph, a line break, or the end of a
|
330 |
|
|
* sentence (in that order of preference).
|
331 |
|
|
*
|
332 |
|
|
* @param $text
|
333 |
|
|
* The content for which a summary will be generated.
|
334 |
|
|
* @param $format
|
335 |
|
|
* The format of the content.
|
336 |
|
|
* If the PHP filter is present and $text contains PHP code, we do not
|
337 |
|
|
* split it up to prevent parse errors.
|
338 |
|
|
* If the line break filter is present then we treat newlines embedded in
|
339 |
|
|
* $text as line breaks.
|
340 |
|
|
* If the htmlcorrector filter is present, it will be run on the generated
|
341 |
|
|
* summary (if different from the incoming $text).
|
342 |
|
|
* @param $size
|
343 |
|
|
* The desired character length of the summary. If omitted, the default
|
344 |
|
|
* value will be used. Ignored if the special delimiter is present
|
345 |
|
|
* in $text.
|
346 |
|
|
* @return
|
347 |
|
|
* The generated summary.
|
348 |
|
|
*/
|
349 |
|
|
function text_summary($text, $format = NULL, $size = NULL) {
|
350 |
|
|
|
351 |
|
|
if (!isset($size)) {
|
352 |
|
|
// What used to be called 'teaser' is now called 'summary', but
|
353 |
|
|
// the variable 'teaser_length' is preserved for backwards compatibility.
|
354 |
|
|
$size = variable_get('teaser_length', 600);
|
355 |
|
|
}
|
356 |
|
|
|
357 |
|
|
// Find where the delimiter is in the body
|
358 |
|
|
$delimiter = strpos($text, '<!--break-->');
|
359 |
|
|
|
360 |
|
|
// If the size is zero, and there is no delimiter, the entire body is the summary.
|
361 |
|
|
if ($size == 0 && $delimiter === FALSE) {
|
362 |
|
|
return $text;
|
363 |
|
|
}
|
364 |
|
|
|
365 |
|
|
// If a valid delimiter has been specified, use it to chop off the summary.
|
366 |
|
|
if ($delimiter !== FALSE) {
|
367 |
|
|
return substr($text, 0, $delimiter);
|
368 |
|
|
}
|
369 |
|
|
|
370 |
|
|
// We check for the presence of the PHP evaluator filter in the current
|
371 |
|
|
// format. If the body contains PHP code, we do not split it up to prevent
|
372 |
|
|
// parse errors.
|
373 |
|
|
if (isset($format)) {
|
374 |
|
|
$filters = filter_list_format($format);
|
375 |
|
|
if (isset($filters['php_code']) && $filters['php_code']->status && strpos($text, '<?') !== FALSE) {
|
376 |
|
|
return $text;
|
377 |
|
|
}
|
378 |
|
|
}
|
379 |
|
|
|
380 |
|
|
// If we have a short body, the entire body is the summary.
|
381 |
|
|
if (drupal_strlen($text) <= $size) {
|
382 |
|
|
return $text;
|
383 |
|
|
}
|
384 |
|
|
|
385 |
|
|
// If the delimiter has not been specified, try to split at paragraph or
|
386 |
|
|
// sentence boundaries.
|
387 |
|
|
|
388 |
|
|
// The summary may not be longer than maximum length specified. Initial slice.
|
389 |
|
|
$summary = truncate_utf8($text, $size);
|
390 |
|
|
|
391 |
|
|
// Store the actual length of the UTF8 string -- which might not be the same
|
392 |
|
|
// as $size.
|
393 |
|
|
$max_rpos = strlen($summary);
|
394 |
|
|
|
395 |
|
|
// How much to cut off the end of the summary so that it doesn't end in the
|
396 |
|
|
// middle of a paragraph, sentence, or word.
|
397 |
|
|
// Initialize it to maximum in order to find the minimum.
|
398 |
|
|
$min_rpos = $max_rpos;
|
399 |
|
|
|
400 |
|
|
// Store the reverse of the summary. We use strpos on the reversed needle and
|
401 |
|
|
// haystack for speed and convenience.
|
402 |
|
|
$reversed = strrev($summary);
|
403 |
|
|
|
404 |
|
|
// Build an array of arrays of break points grouped by preference.
|
405 |
|
|
$break_points = array();
|
406 |
|
|
|
407 |
|
|
// A paragraph near the end of sliced summary is most preferable.
|
408 |
|
|
$break_points[] = array('</p>' => 0);
|
409 |
|
|
|
410 |
|
|
// If no complete paragraph then treat line breaks as paragraphs.
|
411 |
|
|
$line_breaks = array('<br />' => 6, '<br>' => 4);
|
412 |
|
|
// Newline only indicates a line break if line break converter
|
413 |
|
|
// filter is present.
|
414 |
|
|
if (isset($filters['filter_autop'])) {
|
415 |
|
|
$line_breaks["\n"] = 1;
|
416 |
|
|
}
|
417 |
|
|
$break_points[] = $line_breaks;
|
418 |
|
|
|
419 |
|
|
// If the first paragraph is too long, split at the end of a sentence.
|
420 |
|
|
$break_points[] = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1);
|
421 |
|
|
|
422 |
|
|
// Iterate over the groups of break points until a break point is found.
|
423 |
|
|
foreach ($break_points as $points) {
|
424 |
|
|
// Look for each break point, starting at the end of the summary.
|
425 |
|
|
foreach ($points as $point => $offset) {
|
426 |
|
|
// The summary is already reversed, but the break point isn't.
|
427 |
|
|
$rpos = strpos($reversed, strrev($point));
|
428 |
|
|
if ($rpos !== FALSE) {
|
429 |
|
|
$min_rpos = min($rpos + $offset, $min_rpos);
|
430 |
|
|
}
|
431 |
|
|
}
|
432 |
|
|
|
433 |
|
|
// If a break point was found in this group, slice and stop searching.
|
434 |
|
|
if ($min_rpos !== $max_rpos) {
|
435 |
|
|
// Don't slice with length 0. Length must be <0 to slice from RHS.
|
436 |
|
|
$summary = ($min_rpos === 0) ? $summary : substr($summary, 0, 0 - $min_rpos);
|
437 |
|
|
break;
|
438 |
|
|
}
|
439 |
|
|
}
|
440 |
|
|
|
441 |
|
|
// If the htmlcorrector filter is present, apply it to the generated summary.
|
442 |
|
|
if (isset($filters['filter_htmlcorrector'])) {
|
443 |
|
|
$summary = _filter_htmlcorrector($summary);
|
444 |
|
|
}
|
445 |
|
|
|
446 |
|
|
return $summary;
|
447 |
|
|
}
|
448 |
|
|
|
449 |
|
|
/**
|
450 |
|
|
* Implements hook_field_widget_info().
|
451 |
|
|
*/
|
452 |
|
|
function text_field_widget_info() {
|
453 |
|
|
return array(
|
454 |
|
|
'text_textfield' => array(
|
455 |
|
|
'label' => t('Text field'),
|
456 |
|
|
'field types' => array('text'),
|
457 |
|
|
'settings' => array('size' => 60),
|
458 |
|
|
),
|
459 |
|
|
'text_textarea' => array(
|
460 |
|
|
'label' => t('Text area (multiple rows)'),
|
461 |
|
|
'field types' => array('text_long'),
|
462 |
|
|
'settings' => array('rows' => 5),
|
463 |
|
|
),
|
464 |
|
|
'text_textarea_with_summary' => array(
|
465 |
|
|
'label' => t('Text area with a summary'),
|
466 |
|
|
'field types' => array('text_with_summary'),
|
467 |
|
|
'settings' => array('rows' => 20, 'summary_rows' => 5),
|
468 |
|
|
),
|
469 |
|
|
);
|
470 |
|
|
}
|
471 |
|
|
|
472 |
|
|
/**
|
473 |
|
|
* Implements hook_field_widget_settings_form().
|
474 |
|
|
*/
|
475 |
|
|
function text_field_widget_settings_form($field, $instance) {
|
476 |
|
|
$widget = $instance['widget'];
|
477 |
|
|
$settings = $widget['settings'];
|
478 |
|
|
|
479 |
|
|
if ($widget['type'] == 'text_textfield') {
|
480 |
|
|
$form['size'] = array(
|
481 |
|
|
'#type' => 'textfield',
|
482 |
|
|
'#title' => t('Size of textfield'),
|
483 |
|
|
'#default_value' => $settings['size'],
|
484 |
|
|
'#required' => TRUE,
|
485 |
|
|
'#element_validate' => array('element_validate_integer_positive'),
|
486 |
|
|
);
|
487 |
|
|
}
|
488 |
|
|
else {
|
489 |
|
|
$form['rows'] = array(
|
490 |
|
|
'#type' => 'textfield',
|
491 |
|
|
'#title' => t('Rows'),
|
492 |
|
|
'#default_value' => $settings['rows'],
|
493 |
|
|
'#required' => TRUE,
|
494 |
|
|
'#element_validate' => array('element_validate_integer_positive'),
|
495 |
|
|
);
|
496 |
|
|
}
|
497 |
|
|
|
498 |
|
|
return $form;
|
499 |
|
|
}
|
500 |
|
|
|
501 |
|
|
/**
|
502 |
|
|
* Implements hook_field_widget_form().
|
503 |
|
|
*/
|
504 |
|
|
function text_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
|
505 |
|
|
$summary_widget = array();
|
506 |
|
|
$main_widget = array();
|
507 |
|
|
|
508 |
|
|
switch ($instance['widget']['type']) {
|
509 |
|
|
case 'text_textfield':
|
510 |
|
|
$main_widget = $element + array(
|
511 |
|
|
'#type' => 'textfield',
|
512 |
|
|
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
|
513 |
|
|
'#size' => $instance['widget']['settings']['size'],
|
514 |
|
|
'#maxlength' => $field['settings']['max_length'],
|
515 |
|
|
'#attributes' => array('class' => array('text-full')),
|
516 |
|
|
);
|
517 |
|
|
break;
|
518 |
|
|
|
519 |
|
|
case 'text_textarea_with_summary':
|
520 |
|
|
$display = !empty($items[$delta]['summary']) || !empty($instance['settings']['display_summary']);
|
521 |
|
|
$summary_widget = array(
|
522 |
|
|
'#type' => $display ? 'textarea' : 'value',
|
523 |
|
|
'#default_value' => isset($items[$delta]['summary']) ? $items[$delta]['summary'] : NULL,
|
524 |
|
|
'#title' => t('Summary'),
|
525 |
|
|
'#rows' => $instance['widget']['settings']['summary_rows'],
|
526 |
|
|
'#description' => t('Leave blank to use trimmed value of full text as the summary.'),
|
527 |
|
|
'#attached' => array(
|
528 |
|
|
'js' => array(drupal_get_path('module', 'text') . '/text.js'),
|
529 |
|
|
),
|
530 |
|
|
'#attributes' => array('class' => array('text-summary')),
|
531 |
|
|
'#prefix' => '<div class="text-summary-wrapper">',
|
532 |
|
|
'#suffix' => '</div>',
|
533 |
|
|
'#weight' => -10,
|
534 |
|
|
);
|
535 |
|
|
// Fall through to the next case.
|
536 |
|
|
|
537 |
|
|
case 'text_textarea':
|
538 |
|
|
$main_widget = $element + array(
|
539 |
|
|
'#type' => 'textarea',
|
540 |
|
|
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
|
541 |
|
|
'#rows' => $instance['widget']['settings']['rows'],
|
542 |
|
|
'#attributes' => array('class' => array('text-full')),
|
543 |
|
|
);
|
544 |
|
|
break;
|
545 |
|
|
}
|
546 |
|
|
|
547 |
|
|
if ($main_widget) {
|
548 |
|
|
// Conditionally alter the form element's type if text processing is enabled.
|
549 |
|
|
if ($instance['settings']['text_processing']) {
|
550 |
|
|
$element = $main_widget;
|
551 |
|
|
$element['#type'] = 'text_format';
|
552 |
|
|
$element['#format'] = isset($items[$delta]['format']) ? $items[$delta]['format'] : NULL;
|
553 |
|
|
$element['#base_type'] = $main_widget['#type'];
|
554 |
|
|
}
|
555 |
|
|
else {
|
556 |
|
|
$element['value'] = $main_widget;
|
557 |
|
|
}
|
558 |
|
|
}
|
559 |
|
|
if ($summary_widget) {
|
560 |
|
|
$element['summary'] = $summary_widget;
|
561 |
|
|
}
|
562 |
|
|
|
563 |
|
|
return $element;
|
564 |
|
|
}
|
565 |
|
|
|
566 |
|
|
/**
|
567 |
|
|
* Implements hook_field_widget_error().
|
568 |
|
|
*/
|
569 |
|
|
function text_field_widget_error($element, $error, $form, &$form_state) {
|
570 |
|
|
switch ($error['error']) {
|
571 |
|
|
case 'text_summary_max_length':
|
572 |
|
|
$error_element = $element[$element['#columns'][1]];
|
573 |
|
|
break;
|
574 |
|
|
|
575 |
|
|
default:
|
576 |
|
|
$error_element = $element[$element['#columns'][0]];
|
577 |
|
|
break;
|
578 |
|
|
}
|
579 |
|
|
|
580 |
|
|
form_error($error_element, $error['message']);
|
581 |
|
|
}
|
582 |
|
|
|
583 |
|
|
/**
|
584 |
|
|
* Implements hook_field_prepare_translation().
|
585 |
|
|
*/
|
586 |
|
|
function text_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
|
587 |
|
|
// If the translating user is not permitted to use the assigned text format,
|
588 |
|
|
// we must not expose the source values.
|
589 |
|
|
$field_name = $field['field_name'];
|
590 |
|
|
if (!empty($source_entity->{$field_name}[$source_langcode])) {
|
591 |
|
|
$formats = filter_formats();
|
592 |
|
|
foreach ($source_entity->{$field_name}[$source_langcode] as $delta => $item) {
|
593 |
|
|
$format_id = $item['format'];
|
594 |
|
|
if (!empty($format_id) && !filter_access($formats[$format_id])) {
|
595 |
|
|
unset($items[$delta]);
|
596 |
|
|
}
|
597 |
|
|
}
|
598 |
|
|
}
|
599 |
|
|
}
|
600 |
|
|
|
601 |
|
|
/**
|
602 |
|
|
* Implements hook_filter_format_update().
|
603 |
|
|
*/
|
604 |
|
|
function text_filter_format_update($format) {
|
605 |
|
|
field_cache_clear();
|
606 |
|
|
}
|
607 |
|
|
|
608 |
|
|
/**
|
609 |
|
|
* Implements hook_filter_format_disable().
|
610 |
|
|
*/
|
611 |
|
|
function text_filter_format_disable($format) {
|
612 |
|
|
field_cache_clear();
|
613 |
|
|
} |