You are here

function _maxlength_format_element in Maxlength 5.2

Same name and namespace in other branches
  1. 6.2 maxlength.module \_maxlength_format_element()
  2. 7 maxlength.module \_maxlength_format_element()
  3. 7.2 maxlength.module \_maxlength_format_element()
1 call to _maxlength_format_element()
_maxlength_content_form_alter in ./maxlength.module

File

./maxlength.module, line 185

Code

function _maxlength_format_element(&$element, $field, $id, $type = '') {
  $values = _maxlength_get_values($field, $type);
  if ($values !== FALSE and isset($values['limit']) and $values['limit'] and $values['use_js']) {
    $path = drupal_get_path('module', 'maxlength');
    drupal_add_js($path . '/maxlength.js');
    $remaining = $values['limit'] - drupal_strlen($element['#default_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;
    }
    $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'],
      '!remaining' => '<span class="maxlength-counter-remaining">' . $remaining . '</span>',
    )) . '</div>';
  }
}