You are here

function _maxlength_format_element in Maxlength 7.2

Same name and namespace in other branches
  1. 5.2 maxlength.module \_maxlength_format_element()
  2. 6.2 maxlength.module \_maxlength_format_element()
  3. 7 maxlength.module \_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

@arg array $element The form element which should be maxlengthed.

Return value

Maxlength-enabled form element

1 string reference to '_maxlength_format_element'
_maxlength_content_form_alter in ./maxlength.inc
@file Business logic for maxlength

File

./maxlength.module, line 135
Enables a max length countdown on node body, title and CCK textfields.

Code

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;
}