You are here

maxlength.module in Maxlength 7.2

Enables a max length countdown on node body, title and CCK textfields.

File

maxlength.module
View source
<?php

/**
 * @file
 *   Enables a max length countdown on node body, title and CCK textfields.
 */

/**
 * Implementation of hook_help().
 */
function maxlength_help($path, $arg) {
  switch ($path) {
    case 'admin/help#maxlength':
    case 'admin/modules#description':
      return t('Sets a maximum length for body fields and shows a counter that is updated as you type.');
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function maxlength_form_alter(&$form, &$form_state, $form_id) {

  // Editing the content
  if (strpos($form['#id'], 'node-form') !== FALSE) {
    module_load_include('inc', 'maxlength', 'maxlength');
    _maxlength_content_form_alter($form, $form_state, $form_id);
  }
  elseif ($form_id == 'node_type_form' && isset($form['submission'])) {
    module_load_include('inc', 'maxlength', 'maxlength');
    _maxlength_content_type_form_alter($form, $form_state, $form_id);
  }
  elseif ($form_id == 'field_ui_field_edit_form' && isset($form['field']['settings'])) {
    if ($form['#field']['type'] == 'text' || $form['#field']['type'] == 'text_long' || $form['#field']['type'] == 'text_with_summary') {
      if (($form['#field']['type'] == 'text_long' || $form['#field']['type'] == 'text_with_summary') && empty($form['field']['settings']['max_length'])) {
        $field_key = $form['instance']['field_name']['#value'];
        if (!(strpos($field_key, 'field_') === 0)) {
          $type = isset($form['instance']['bundle']['#value']) ? $form['instance']['bundle']['#value'] : '';
          if ($type != '') {
            $field_key .= '_' . $type;
          }
        }
        $form['field']['settings']['max_length'] = array(
          '#type' => 'textfield',
          '#title' => t('Maximum length'),
          '#default_value' => variable_get('maxlength_' . $field_key, ''),
          '#required' => FALSE,
          '#element_validate' => array(
            '_element_validate_integer_positive',
          ),
          '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
        );
      }
      module_load_include('inc', 'maxlength', 'maxlength');
      _maxlength_cck_form_alter($form, $form_state, $form_id);
    }
  }
  elseif ($form_id == '_content_admin_field_remove') {
    variable_del('maxlength_' . $form['field_name']['#value']);
    variable_del('maxlength_' . $form['field_name']['#value'] . '_js');
    variable_del('maxlength_' . $form['field_name']['#value'] . '_text');
  }
}

/**
 * Implementation of hook_node_validate().
 */
function maxlength_node_validate($node, $form, &$form_state) {

  // Core fields, CCK takes care of CCK fields, no need to validate them
  $fields = array(
    'title',
  );
  foreach ($fields as $field) {
    $limit = intval(variable_get('maxlength_' . $field . '_' . $node->type, ''));
    if ($limit > 0) {

      //$form = $a3;

      // line breaks can be stored 2 or more chars, breaking the count.
      $text = $node->{$field};
      $text = str_replace("\r\n", '#', $text);
      $text = str_replace("\n", '#', $text);
      $text = str_replace("\r", '#', $text);
      if (drupal_strlen($text) > $limit) {
        form_set_error($field, t('The @field field has exceeded its maximum number of characters (@limit).', array(
          '@limit' => $limit,
          '@field' => $field,
        )));
      }
    }
  }
}

/**
 * Implements hook_node_type_update().
 */
function maxlength_node_type_delete($info) {
  $labels = _maxlength_node_type_get_labels();
  foreach ($labels as $label) {
    variable_del('maxlength_' . $label . $info->type);
  }
}

/**
 * Implements hook_node_type_update().
 */
function maxlength_node_type_update($info) {
  $labels = _maxlength_node_type_get_labels();
  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);
    }
  }
}
function _maxlength_node_type_get_labels() {
  $labels = array(
    'title',
    'js_title',
    'text_title',
    'body',
    'js_body',
    'text_body',
  );
  return $labels;
}

/**
 * Formats a form element to use maxlength value and use js.
 * It's not moved to maxlength.inc because Form API calls it even when form_alter is not called
 *
 * @arg array $element
 *   The form element which should be maxlengthed.
 * @return
 *   Maxlength-enabled form element
 *
 */
function _maxlength_format_element($element) {
  static $processed = array();
  module_load_include('inc', 'maxlength', 'maxlength');
  $value = $element['#max_length_properties'][0];
  $field = $element['#max_length_properties'][1];
  $id = $element['#max_length_properties'][2];
  $type = $element['#max_length_properties'][3];
  if (in_array('edit-' . $id, $processed)) {
    return $element;
  }
  $values = _maxlength_get_values($field, $type);
  if ($values !== FALSE && isset($values['limit']) && $values['limit'] && $values['use_js']) {
    $path = drupal_get_path('module', 'maxlength');
    drupal_add_js($path . '/maxlength.js');
    $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['#default_value'] = drupal_substr($element['#default_value'], 0, $values['limit']);
      $remaining = 0;
    }
    if (in_array($element['#id'], $processed) === FALSE) {
      $js_settings = array(
        'maxlength' => array(
          'edit-' . $id => $values['limit'],
        ),
      );
      drupal_add_js($js_settings, 'setting');
    }
    $element['#suffix'] = '<div id="maxlength-' . $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>';
    $processed[] = $element['#id'];
  }
  return $element;
}

Functions

Namesort descending Description
maxlength_form_alter Implementation of hook_form_alter().
maxlength_help Implementation of hook_help().
maxlength_node_type_delete Implements hook_node_type_update().
maxlength_node_type_update Implements hook_node_type_update().
maxlength_node_validate Implementation of hook_node_validate().
_maxlength_format_element Formats a form element to use maxlength value and use js. It's not moved to maxlength.inc because Form API calls it even when form_alter is not called
_maxlength_node_type_get_labels