View source
<?php
define('MAXLENGTH_DEFAULT_TEXT', 'Content limited to !limit characters, remaining: <strong>!remaining</strong>');
define('MAXLENGTH_DEFAULT_USE_JS', TRUE);
define('MAXLENGTH_DEFAULT_LENGTH', 0);
function maxlength_help($path, $arg) {
switch ($path) {
case 'admin/help#max_length_properties':
case 'admin/modules#description':
return t('Sets a maximum length for body fields and shows a counter that is updated as you type.');
break;
}
}
function maxlength_form_alter(&$form, &$form_state, $form_id) {
$form['#after_build'][] = 'maxlength_after_build';
if ($form['#id'] == 'node-form') {
module_load_include('inc', 'maxlength');
_maxlength_content_form_alter($form, $form_state, $form_id);
}
elseif ($form['#id'] == 'comment-form') {
module_load_include('inc', 'maxlength');
_maxlength_comment_form_alter($form, $form_state, $form_id);
}
elseif ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
module_load_include('inc', 'maxlength');
_maxlength_content_type_form_alter($form, $form_state, $form_id);
}
elseif ($form_id == 'content_field_edit_form') {
module_load_include('inc', 'maxlength');
if (_maxlength_is_supported_widget($form['#field']['widget']['type'])) {
_maxlength_cck_form_alter($form, $form_state, $form_id);
}
}
elseif ($form_id == 'content_field_remove_form') {
$form['#submit'][] = 'maxlength_field_remove_submit';
}
}
function maxlength_field_remove_submit($form, &$form_state) {
maxlength_remove_field_variables($form_state['values']['field_name']);
}
function maxlength_remove_field_variables($field_name) {
variable_del('maxlength_' . $field_name);
variable_del('maxlength_' . $field_name . '_js');
variable_del('maxlength_' . $field_name . '_text');
}
function maxlength_node_type($op, $info) {
$labels = array(
'title',
'js_title',
'text_title',
'body',
'js_body',
'text_body',
);
switch ($op) {
case 'delete':
foreach ($labels as $label) {
variable_del('maxlength_' . $label . $info->type);
}
break;
case 'update':
if (!empty($info->old_type) && $info->old_type != $info->type) {
foreach ($labels as $label) {
$old_var = variable_get('maxlength_' . $label . $info->old_type, '');
variable_set('maxlength_' . $label . $info->type, $old_var);
variable_del('maxlength_' . $label . $info->old_type);
}
}
break;
}
}
function maxlength_after_build($element) {
maxlength_recursive($element);
return $element;
}
function maxlength_recursive(&$elements) {
foreach (element_children($elements) as $key) {
if (isset($elements[$key]) && $elements[$key]) {
if (!empty($elements[$key]['#max_length_properties'])) {
_maxlength_format_element($elements[$key]);
}
maxlength_recursive($elements[$key]);
}
}
}
function _maxlength_format_element(&$element) {
static $js_added = FALSE;
if (!empty($element['#max_length_properties']['key'])) {
if (!empty($element[$element['#max_length_properties']['key']])) {
$element[$element['#max_length_properties']['key']]['#max_length_properties'] = $element['#max_length_properties'];
unset($element[$element['#max_length_properties']['key']]['#max_length_properties']['key']);
unset($element['#max_length_properties']);
}
return;
}
if (!is_array($element['#max_length_properties'])) {
$element['#max_length_properties'] = array(
'limit' => $element['#max_length_properties'],
);
}
$element['#max_length_properties'] += array(
'text' => MAXLENGTH_DEFAULT_TEXT,
'use_js' => MAXLENGTH_DEFAULT_USE_JS,
'value_key' => 'default_value',
);
$element['#element_validate'][] = 'maxlength_validate_element';
$values = $element['#max_length_properties'];
if ($values['use_js']) {
if (empty($js_added)) {
$path = drupal_get_path('module', 'maxlength');
drupal_add_js($path . '/maxlength.js');
$js_added = TRUE;
}
$value = !empty($element[$values['value_key']]) ? $element[$values['value_key']] : '';
$remaining = $values['limit'] - drupal_strlen($value);
if ($remaining < 0) {
drupal_set_message(t('%body_field_label truncated to %limit characters!', array(
'%body_field_label' => $element['#title'],
'%limit' => $values['limit'],
)), 'error');
$element[$values['value_key']] = drupal_substr($element['#default_value'], 0, $values['limit']);
$remaining = 0;
}
if (!isset($element['#id'])) {
$element['#id'] = form_clean_id('edit-' . implode('-', $element['#parents']));
}
if (empty($element['#maxlength_processed'])) {
$js_settings = array(
'maxlength' => array(
$element['#id'] => $values['limit'],
),
);
drupal_add_js($js_settings, 'setting');
$element['#maxlength_processed'] = TRUE;
}
$element['#suffix'] = '<div id="maxlength-' . $element['#id'] . '"
class="maxlength-counter">' . t($values['text'], array(
'!limit' => $values['limit'],
'!count' => '<span class="maxlength-count">' . drupal_strlen($value) . '</span>',
'!remaining' => '<span class="maxlength-counter-remaining">' . $remaining . '</span>',
)) . '</div>';
}
}
function maxlength_validate_element($element, &$form_state) {
$text = $element['#value'];
$text = str_replace("\r\n", '#', $text);
$text = str_replace(array(
"\n",
"\r",
), '#', $text);
if (drupal_strlen($text) > $element['#max_length_properties']['limit']) {
form_error($element, t('!field has exceeded its maximum number of characters (!limit).', array(
'!limit' => $element['#max_length_properties']['limit'],
'!field' => $element['#title'],
)));
}
}