You are here

class MaxLengthCallbacks in Maxlength 8

Trusted render callbacks.

Hierarchy

Expanded class hierarchy of MaxLengthCallbacks

1 file declares its use of MaxLengthCallbacks
maxlength.module in ./maxlength.module
Limits the number of characters in textfields and textareas and shows the amount of characters left.

File

src/MaxLengthCallbacks.php, line 11

Namespace

Drupal\maxlength
View source
class MaxLengthCallbacks implements TrustedCallbackInterface {
  public static function trustedCallbacks() {
    return [
      'maxlengthPreRender',
      'processElement',
    ];
  }

  /**
   * Pre render function to set maxlength attributes.
   *
   * @param array $element
   *   The render array.
   *
   * @return array
   *   The processed render array.
   */
  public static function maxlengthPreRender($element) {
    if (isset($element['#maxlength_js']) && $element['#maxlength_js'] === TRUE) {
      if (isset($element['#attributes']['maxlength']) && $element['#attributes']['maxlength'] > 0) {
        $element['#attributes']['class'][] = 'maxlength';
        $element['#attached']['library'][] = 'maxlength/maxlength';
      }
      if (isset($element['summary']['#attributes']['maxlength']) && $element['summary']['#attributes']['maxlength'] > 0) {
        $element['summary']['#attributes']['class'][] = 'maxlength';
        $element['summary']['#attached']['library'][] = 'maxlength/maxlength';
      }
    }
    return $element;
  }

  /**
   * Process handler for the form elements that can have maxlength attribute.
   *
   * @param array $element
   *   The render array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   *
   * @return array
   *   The processed render array.
   */
  public static function processElement($element, FormStateInterface $form_state) {
    if (isset($element['#attributes']['#maxlength_js_enforce']) && $element['#attributes']['#maxlength_js_enforce']) {
      $element['#attributes']['class'][] = 'maxlength_js_enforce';
    }
    if (isset($element['#attributes']['#maxlength_js_truncate_html']) && $element['#attributes']['#maxlength_js_truncate_html']) {
      $element['#attributes']['class'][] = 'maxlength_js_truncate_html';
    }
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MaxLengthCallbacks::maxlengthPreRender public static function Pre render function to set maxlength attributes.
MaxLengthCallbacks::processElement public static function Process handler for the form elements that can have maxlength attribute.
MaxLengthCallbacks::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.