View source
<?php
define('MAXLENGTH_DEFAULT_JS_LABEL', 'Content limited to @limit characters, remaining: <strong>@remaining</strong>');
function maxlength_help($path, $arg) {
switch ($path) {
case 'admin/help#maxlength':
$output = '<p>' . t('Maxlength creates a new Form Property: #maxlength_js which in conjunction with #maxlength will enforce, via JavaScript, the maxlength of a textfield or textarea and will show the number of characters left.') . '</p>';
$output .= '<p>' . t('Using the field setting page, you can limit textfields and textareas. For textfields this module will use the "<strong>Maximum length</strong>" value set in the <strong>field settings</strong>.') . '</p>';
return $output;
}
}
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."),
),
);
}
function maxlength_element_info_alter(&$cache) {
$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';
}
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'][] = drupal_get_path('module', 'maxlength') . '/js/maxlength.js';
}
return $element;
}
function maxlength_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
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.'),
);
}
$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,
),
),
),
);
}
$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,
);
}
$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.'),
);
}
}
function maxlength_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
$fields = field_info_instances($entity_type, $form['#bundle']);
foreach ($fields as $field_name => $value) {
if (isset($value['widget']['settings']['maxlength_js'])) {
$elements[$field_name] = $value;
}
}
if (isset($elements)) {
_maxlength_children($form, $elements);
}
}
function _maxlength_children(&$element, $ms_elements) {
$bypass =& drupal_static(__FUNCTION__);
if (isset($bypass) && $bypass) {
return;
}
elseif (!isset($bypass)) {
$bypass = $GLOBALS['user']->uid == 1 ? !variable_get('maxlength_always_for_uid1', FALSE) : user_access('bypass maxlength');
}
$children = element_get_visible_children($element);
foreach ($children as $child) {
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);
}
}
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,
);
}
function maxlength_process_element($element, &$form_state) {
if (isset($element['#maxlength_js_enforce']) && $element['#maxlength_js_enforce']) {
$element['#attributes']['class'][] = 'maxlength_js_enforce';
}
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;
}
function maxlength_validate_input(&$element, &$form_state) {
if (isset($element['#attributes']['maxlength']) && isset($element['#value'])) {
$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 (drupal_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' => drupal_strlen($value),
)));
}
$element['#maxlength'] = $element['#attributes']['maxlength'];
}
}
function maxlength_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['#node']) && strpos($form_id, '_node_form') !== FALSE && variable_get('maxlength_js_' . $form['#node']->type, FALSE)) {
if (!module_exists('title') || title_field_replacement_enabled('node', $form['#node']->type, 'title') !== TRUE) {
$form['title']['#maxlength_js'] = TRUE;
$form['title']['#maxlength'] = intval(variable_get('maxlength_js_' . $form['#node']->type, ''));
$form['title']['#attributes']['maxlength_js_label'] = array();
$maxlength_js_label = t(variable_get('maxlength_js_label_' . $form['#node']->type, MAXLENGTH_DEFAULT_JS_LABEL));
$form['title']['#attributes']['maxlength_js_label'][] = $maxlength_js_label;
maxlength_pre_render($form['title']);
}
}
}
function maxlength_form_node_type_form_alter(&$form, &$form_state, $form_id) {
$type = $form['#node_type']->type;
if (!module_exists('title') || title_field_replacement_enabled('node', $type, 'title') !== TRUE) {
$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' => variable_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' => variable_get('maxlength_js_label_' . $form['#node_type']->type, 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.'),
);
}
}
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.'));
}
}