You are here

function _maxlength_format_element in Maxlength 6.2

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

1 call to _maxlength_format_element()
maxlength_recursive in ./maxlength.module
Finds all the elements in the form that request #max_length_properties behaviour and respond.

File

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

Code

function _maxlength_format_element(&$element) {
  static $js_added = FALSE;

  // Allow using a child element instead. The recurision will process
  // this later.
  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;
  }

  // Allow for a short hand method.
  if (!is_array($element['#max_length_properties'])) {
    $element['#max_length_properties'] = array(
      'limit' => $element['#max_length_properties'],
    );
  }

  // Set the default values.
  $element['#max_length_properties'] += array(
    'text' => MAXLENGTH_DEFAULT_TEXT,
    'use_js' => MAXLENGTH_DEFAULT_USE_JS,
    'value_key' => 'default_value',
  );

  // Add in validator.
  $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;
    }

    // Make sure #id was set
    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>';
  }
}