MaxLengthCallbacks.php in Maxlength 8
File
src/MaxLengthCallbacks.php
View source
<?php
namespace Drupal\maxlength;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
class MaxLengthCallbacks implements TrustedCallbackInterface {
public static function trustedCallbacks() {
return [
'maxlengthPreRender',
'processElement',
];
}
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;
}
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;
}
}