TextareaWithCounterWidget.php in Textfield Counter 8
File
src/Plugin/Field/FieldWidget/TextareaWithCounterWidget.php
View source
<?php
namespace Drupal\textfield_counter\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\text\Plugin\Field\FieldWidget\TextareaWidget;
class TextareaWithCounterWidget extends TextareaWidget {
use TextFieldCounterWidgetTrait;
public static function defaultSettings() {
return [
'maxlength' => 0,
'counter_position' => 'after',
'js_prevent_submit' => TRUE,
'count_html_characters' => TRUE,
'textcount_status_message' => self::getDefaultTextCountStatusMessage(),
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$this
->addMaxlengthSettingsFormElement($form);
$this
->addCounterPositionSettingsFormElement($form);
$this
->addJsPreventSubmitSettingsFormElement($form);
$this
->addCountHtmlSettingsFormElement($form);
$this
->addTextCountStatusMessageSettingsFormElement($form);
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$this
->addMaxlengthSummary($summary);
$this
->addPositionSummary($summary);
$this
->addJsSubmitPreventSummary($summary);
$this
->addCountHtmlSummary($summary);
$this
->addTextCountStatusMessageSummary($summary);
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
if ($maxlength = $this
->getSetting('maxlength')) {
$entity = $items
->getEntity();
$field_defintion = $items
->getFieldDefinition();
$this
->fieldFormElement($element, $entity, $field_defintion, $delta);
$count_html_characters = $this
->getSetting('count_html_characters');
if (isset($element['value'])) {
$element['value']['#textfield-maxlength'] = $maxlength;
$element['value']['#textfield-count-html'] = $count_html_characters;
}
$element['#textfield-maxlength'] = $maxlength;
$element['#textfield-count-html'] = $count_html_characters;
$classes = class_uses($this);
if (count($classes)) {
$element['#element_validate'][] = [
array_pop($classes),
'validateFieldFormElement',
];
}
}
return $element;
}
}