-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaxlength.module
312 lines (289 loc) · 14.1 KB
/
maxlength.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
303
304
305
306
307
308
309
310
311
312
<?php
/**
* @file
* Limit the number of characters in textfields and textareas and shows the amount of characters left.
*/
define('MAXLENGTH_DEFAULT_JS_LABEL', 'Content limited to @limit characters, remaining: <strong>@remaining</strong>');
/**
* Implements hook_permission().
*/
function maxlength_permission() {
return array(
'bypass maxlength' => array(
'title' => t('Bypass maxlength setting'),
'description' => t("If a maxlength is configured for a certain field, the user don't see the value."),
),
);
}
/**
* Implements hook_element_info_alter().
*/
function maxlength_element_info_alter(&$cache) {
// Add prerender functions to textareas and text fields.
$cache['textfield']['#pre_render'][] = 'maxlength_pre_render';
$cache['textarea']['#pre_render'][] = 'maxlength_pre_render';
$cache['textarea']['#process'][] = 'maxlength_process_element';
$cache['text_format']['#pre_render'][] = 'maxlength_pre_render';
$cache['text_format']['#process'][] = 'maxlength_process_element';
}
/**
* Pre render function to set maxlength attributes.
*/
function maxlength_pre_render($element) {
if (((isset($element['#maxlength']) && $element['#maxlength'] > 0) || (isset($element['#attributes']['maxlength']) && $element['#attributes']['maxlength'] > 0)) &&
isset($element['#maxlength_js']) && $element['#maxlength_js'] === TRUE) {
if ($element['#type'] == 'textarea' && !isset($element['#attributes']['maxlength'])) {
$element['#attributes']['maxlength'] = $element['#maxlength'];
}
$element['#attributes']['class'][] = 'maxlength';
$element['#attached']['js'][] = backdrop_get_path('module', 'maxlength') . '/js/maxlength.js';
}
return $element;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function maxlength_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
// Add an optional maxlength for teaser field.
if ($form['#instance']['widget']['type'] == 'text_textarea_with_summary') {
$form['instance']['widget']['settings']['maxlength_js_summary'] = array(
'#type' => 'textfield',
'#title' => 'Summary Maxlength JS',
'#description' => t('The maximum length of the field in characters.'),
'#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_summary']) ? $form['#instance']['widget']['settings']['maxlength_js_summary'] : NULL,
);
$form['instance']['widget']['settings']['maxlength_js_label_summary'] = array(
'#type' => 'textarea',
'#rows' => 2,
'#title' => t('Summary count down message'),
'#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_label_summary']) ? $form['#instance']['widget']['settings']['maxlength_js_label_summary'] : MAXLENGTH_DEFAULT_JS_LABEL,
'#description' => t('The text used in the Javascript message under the input, where "@limit", "@remaining" and "@count" are replaced by the appropriate numbers.'),
);
}
// Add settings for textarea widgets.
$fields = array('text_textarea_with_summary', 'text_textarea');
if (in_array($form['#instance']['widget']['type'], $fields)) {
$form['instance']['widget']['settings']['maxlength_js'] = array(
'#type' => 'textfield',
'#title' => 'Maxlength JS',
'#description' => t('The maximum length of the field in characters.'),
'#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js']) ? $form['#instance']['widget']['settings']['maxlength_js'] : NULL,
);
$form['instance']['widget']['settings']['maxlength_js_enforce'] = array(
'#type' => 'checkbox',
'#title' => t('Force text truncate'),
'#description' => t('Check this option if you want that the html (or the text) that the user inserts into the field to be truncated.'),
'#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_enforce']) ? $form['#instance']['widget']['settings']['maxlength_js_enforce'] : NULL,
);
$form['instance']['widget']['settings']['maxlength_js_truncate_html'] = array(
'#type' => 'checkbox',
'#title' => t('Truncate html'),
'#description' => t('Check this option if the input field may contain html text and you want to truncate it safely. This will also overwrite the maxlength validation from core, so that it will strip the tags before checking the length.'),
'#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_truncate_html']) ? $form['#instance']['widget']['settings']['maxlength_js_truncate_html'] : NULL,
'#states' => array(
'enabled' => array(
':input[id=edit-instance-widget-settings-maxlength-js-cut-text]' => array('checked' => TRUE),
),
),
);
}
// Add settings for textfield widgets.
$fields = array('text_textfield');
if (in_array($form['#instance']['widget']['type'], $fields)) {
$form['instance']['widget']['settings']['maxlength_js'] = array(
'#type' => 'checkbox',
'#title' => 'Maxlength JS',
'#description' => t('Limit the maximum length of the field in characters using the "<strong>Maximum length</strong>" value set in the <strong>field settings</strong> using Javascript.'),
'#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js']) ? $form['#instance']['widget']['settings']['maxlength_js'] : NULL,
);
}
// Add settings for the maxlength widget label, only for allowed fields.
$fields = array('text_textfield', 'text_textarea_with_summary', 'text_textarea');
if (in_array($form['#instance']['widget']['type'], $fields)) {
$form['instance']['widget']['settings']['maxlength_js_label'] = array(
'#type' => 'textarea',
'#rows' => 2,
'#title' => t('Count down message'),
'#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_label']) ? $form['#instance']['widget']['settings']['maxlength_js_label'] : MAXLENGTH_DEFAULT_JS_LABEL,
'#description' => t('The text used in the Javascript message under the input, where "@limit", "@remaining" and "@count" are replaced by the appropriate numbers.'),
);
}
}
/**
* Implements hook_field_attach_form().
*/
function maxlength_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
$fields = field_info_instances($entity_type, $form['#bundle']);
// Check if any of the fields has a #maxlength_js setting.
foreach ($fields as $field_name => $value) {
if (isset($value['widget']['settings']['maxlength_js'])) {
$elements[$field_name] = $value;
}
}
if (isset($elements)) {
_maxlength_children($form, $elements);
}
}
/**
* Implements hook_config_info().
*/
function maxlength_config_info() {
$prefixes['maxlength.settings'] = array(
'label' => t('MaxLength settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Recursively add the #maxlength_js and #maxlength properties to the elements
* of a form.
*
* @todo: This function is currently only working for fieldapi fields.
*
* @param array $element
* The form element to start looking for.
* @param array $ms_elements
* An associative array of api field elements as returned by
* field_info_instances() that the #maxlength and #maxlength_js properties
* should be set, with the field_name as a key and the field_data as the value.
*/
function _maxlength_children(&$element, $ms_elements) {
$bypass = &backdrop_static(__FUNCTION__);
// Add "$conf['maxlength_always_for_uid1'] = TRUE;" to settings.php to activate
// for user #1.
if (isset($bypass) && $bypass) {
return;
}
elseif (!isset($bypass)) {
$bypass = $GLOBALS['user']->uid == 1 ? !config_get('maxlength.settings', 'maxlength_always_for_uid1') : user_access('bypass maxlength');
}
$children = element_get_visible_children($element);
foreach ($children as $child) {
// Check if the field settings for maxlength_js are set and add the maxlength and the label text.
if (isset($element[$child]['#field_name']) && isset($ms_elements[$element[$child]['#field_name']])) {
$settings = $ms_elements[$element[$child]['#field_name']]['widget']['settings'];
if ($settings['maxlength_js'] > 0) {
_maxlength_add_maxlength_attributes($element[$child], $settings);
if (isset($settings['maxlength_js_enforce']) && $settings['maxlength_js_enforce']) {
$element[$child]['#maxlength_js_enforce'] = TRUE;
}
if (isset($settings['maxlength_js_truncate_html']) && $settings['maxlength_js_truncate_html']) {
$element[$child]['#maxlength_js_truncate_html'] = TRUE;
}
}
if (isset($element[$child]['summary']) && isset($settings['maxlength_js_summary'])
&& $settings['maxlength_js_summary'] > 0) {
_maxlength_add_maxlength_attributes($element[$child]['summary'], $settings, '_summary');
}
}
_maxlength_children($element[$child], $ms_elements);
}
}
/**
* Add maxlength attributes.
*/
function _maxlength_add_maxlength_attributes(&$element, $settings, $suffix = '') {
$element['#maxlength'] = isset($element['#maxlength']) ? $element['#maxlength'] : $settings['maxlength_js' . $suffix];
$element['#maxlength_js'] = TRUE;
$maxlength_js_label = !empty($settings['maxlength_js_label']) ? $settings['maxlength_js_label' . $suffix] : MAXLENGTH_DEFAULT_JS_LABEL;
$maxlength_js_label = t($maxlength_js_label);
$element['#attributes']['maxlength_js_label'] = array($maxlength_js_label);
}
/**
* Process handler for the form elements that can have maxlength attribute.
*/
function maxlength_process_element($element, &$form_state) {
if (isset($element['#maxlength_js_enforce']) && $element['#maxlength_js_enforce']) {
$element['#attributes']['class'][] = 'maxlength_js_enforce';
}
// Move the maxlength property in the attributes of the fields to bypass the
// core validation if we have to truncate the html text.
// We will do our own validation in this case.
if (isset($element['#maxlength_js_truncate_html']) && $element['#maxlength_js_truncate_html']) {
$element['#element_validate'][] = 'maxlength_validate_input';
$element['#attributes']['maxlength'] = $element['#maxlength'];
$element['#attributes']['class'][] = 'maxlength_js_truncate_html';
unset($element['#maxlength']);
}
return $element;
}
/**
* Custom validation handler for the maxlength of input fields that have the
* maxlength attribute.
*/
function maxlength_validate_input(&$element, &$form_state) {
// Verify that the value is not longer than #maxlength characters.
if (isset($element['#attributes']['maxlength']) && isset($element['#value'])) {
// Compute the length of the text, without counting the tags, and consider
// the "enter" characters as only one character.
$value = filter_xss(str_replace(array("\r\n", ' '), array(' ', ' '), $element['#value']), array());
$value = strip_tags($value);
$value = preg_replace("/&#?[a-z0-9]+;/i", "1", $value);
if (backdrop_strlen($value) > $element['#attributes']['maxlength']) {
form_error($element, t('!name cannot be longer than %max characters but is currently %length characters long.', array('!name' => empty($element['#title']) ? $element['#parents'][0] : $element['#title'], '%max' => $element['#attributes']['maxlength'], '%length' => backdrop_strlen($value))));
}
// Giving the element back the #maxlength, maybe some other modules requires it.
$element['#maxlength'] = $element['#attributes']['maxlength'];
}
}
/**
* Implements hook_form_alter().
*/
function maxlength_form_alter(&$form, &$form_state, $form_id) {
$config = config('maxlength.settings');
// Attach maxlength to node title.
if (isset($form['#node']) && strpos($form_id, '_node_form') !== FALSE && $config->get('maxlength_js_' . $form['#node']->type)) {
if (!module_exists('title') || title_field_replacement_enabled('node', $form['#node']->type, 'title') !== TRUE) {
$form['title']['#maxlength_js'] = TRUE;
$form['title']['#maxlength'] = intval($config->get('maxlength_js_' . $form['#node']->type));
$form['title']['#attributes']['maxlength_js_label'] = array();
$maxlength_js_label = t($config->get('maxlength_js_label_' . $form['#node']->type));
$form['title']['#attributes']['maxlength_js_label'][] = $maxlength_js_label;
maxlength_pre_render($form['title']);
}
}
}
/**
* Implements hook_form_node_type_alter().
*/
function maxlength_form_node_type_form_alter(&$form, &$form_state, $form_id) {
$config = config('maxlength.settings');
$type = $form['#node_type']->type;
// If the Title Module is enabled and the Title Field is Replaced the maxlength Module
// we don't need this functionality any more, because the title is now a field itself.
if (!module_exists('title') || title_field_replacement_enabled('node', $type, 'title') !== TRUE) {
// Add maxlength setting to node type form.
$form['submission']['maxlength_js'] = array(
'#type' => 'textfield',
'#title' => 'Maxlength JS',
'#description' => t('The maximum length of the field in characters. Can be maximum 255 characters.'),
'#default_value' => $config->get('maxlength_js_' . $form['#node_type']->type),
'#element_validate' => array('maxlength_node_title_validate'),
);
$form['submission']['maxlength_js_label'] = array(
'#type' => 'textarea',
'#rows' => 2,
'#title' => t('Count down message'),
'#default_value' => $config->get('maxlength_js_label_' . $form['#node_type']->type),
'#description' => t('The text used in the Javascript message under the input, where "@limit", "@remaining" and "@count" are replaced by the appropriate numbers.'),
);
}
}
/**
* Checks if the title field of the node is not set to more then 255 chars, because Backdrop Core cannot handle this.
*/
function maxlength_node_title_validate($element, &$form_state, $form) {
if (!empty($element['#value']) && !is_numeric($element['#value'])) {
form_error($element, t('This field needs to be numeric'));
}
if (!empty($element['#value']) && is_numeric($element['#value']) && $element['#value'] > 255) {
form_error($element, t('Note titles can be maximum 255 characters long.'));
}
}
/**
* Implements hook_autoload_info().
*/
function maxlength_autoload_info() {
return array(
);
}