You are here

function theme_maxlength_textarea in Maxlength 6

Same name and namespace in other branches
  1. 5 maxlength.module \theme_maxlength_textarea()
1 theme call to theme_maxlength_textarea()
maxlength_elements in ./maxlength.module
Implemenation of hook_elements().

File

./maxlength.module, line 125

Code

function theme_maxlength_textarea($element) {
  $prefix = '';
  if ($element['#name'] == 'body') {
    $path = drupal_get_path('module', 'maxlength');
    drupal_add_js($path . '/maxlength.js');
    if (arg(1) == 'add') {
      $type = str_replace('-', '_', arg(2));
    }
    else {
      $type = _maxlength_node_type_from_id(intval(arg(1)));
    }
    $limit = intval(variable_get(MAXLENGTH_NODE_TYPE . $type . '_b', ''));
    $remaining = $limit - drupal_strlen($element['#value']);
    if ($remaining < 0) {
      drupal_set_message(t('%body_field_label truncated to %limit characters!', array(
        '%body_field_label' => $element['#title'],
        '%limit' => $limit,
      )), 'error');
      $element['#value'] = drupal_substr($element['#value'], 0, $limit);
      $remaining = 0;
    }
    $element['#attributes']['onchange'] = 'maxlength_limit(this, ' . $limit . ');';
    $element['#attributes']['onkeyup'] = 'maxlength_limit(this, ' . $limit . ');';
    $prefix = t('<div id="maxlength-counter">Content limited to !limit characters, remaining: <strong id="maxlength-counter-remaining">!remaining</strong></div>', array(
      '!limit' => $limit,
      '!remaining' => $remaining,
    ));
  }
  return theme_textarea($element) . $prefix;
}