You are here

public function TextFieldCounterWidgetTrait::fieldFormElement in Textfield Counter 8

Sets up the form element with the textfield counter.

Parameters

array $element: The render array for the element to which files are being attached.

\Drupal\Core\Entity\EntityInterface $entity: The entity to which the field is attached.

|Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition: The field definition.

int $delta: The delta (index) of the current item.

bool $summary: Set this to TRUE for the summary field on textfields with the summary enabled.

5 calls to TextFieldCounterWidgetTrait::fieldFormElement()
StringTextareaWithCounterWidget::formElement in src/Plugin/Field/FieldWidget/StringTextareaWithCounterWidget.php
Returns the form for a single field widget.
StringTextfieldWithCounterWidget::formElement in src/Plugin/Field/FieldWidget/StringTextfieldWithCounterWidget.php
Returns the form for a single field widget.
TextareaWithCounterWidget::formElement in src/Plugin/Field/FieldWidget/TextareaWithCounterWidget.php
Returns the form for a single field widget.
TextareaWithSummaryAndCounterWidget::formElement in src/Plugin/Field/FieldWidget/TextareaWithSummaryAndCounterWidget.php
Returns the form for a single field widget.
TextfieldWithCounterWidget::formElement in src/Plugin/Field/FieldWidget/TextfieldWithCounterWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/TextFieldCounterWidgetTrait.php, line 293

Class

TextFieldCounterWidgetTrait
Textfield counter trait. Adds textfield counting functionality.

Namespace

Drupal\textfield_counter\Plugin\Field\FieldWidget

Code

public function fieldFormElement(array &$element, EntityInterface $entity, FieldDefinitionInterface $fieldDefinition, $delta, $summary = FALSE) {
  if ($summary) {
    $maxlength = $this
      ->getSetting('summary_maxlength');
  }
  else {
    $maxlength = $this
      ->getSetting('use_field_maxlength') ? $this
      ->getFieldSetting('max_length') : $this
      ->getSetting('maxlength');
  }
  $position = $this
    ->getSetting('counter_position');
  $keys = [
    $entity
      ->getEntityTypeId(),
  ];
  $keys[] = $entity
    ->id() ? $entity
    ->id() : 0;
  if (method_exists($fieldDefinition, 'id')) {
    $field_definition_id = str_replace('.', '--', $fieldDefinition
      ->id());
  }
  else {
    $field_definition_id = "{$entity->getEntityTypeId()}--{$entity->bundle()}--{$fieldDefinition->getName()}";
  }
  $keys[] = $field_definition_id;
  $keys[] = $delta;
  $key = implode('-', $keys);
  if ($summary) {
    $key .= '-summary';
  }
  $element['#attributes']['class'][] = $key;
  $element['#attributes']['class'][] = 'textfield-counter-element';
  $element['#attributes']['data-field-definition-id'] = $field_definition_id;
  $element['#attached']['library'][] = 'textfield_counter/counter';
  $element['#attached']['drupalSettings']['textfieldCounter'][$key]['key'][$delta] = $key;
  $element['#attached']['drupalSettings']['textfieldCounter'][$key]['maxlength'] = (int) $maxlength;
  $element['#attached']['drupalSettings']['textfieldCounter'][$key]['counterPosition'] = $position;
  $element['#attached']['drupalSettings']['textfieldCounter'][$key]['textCountStatusMessage'] = $this
    ->getSetting('textcount_status_message');
  if ($this
    ->getSetting('js_prevent_submit')) {
    $element['#attached']['drupalSettings']['textfieldCounter'][$key]['preventSubmit'] = TRUE;
  }
  $element['#attached']['drupalSettings']['textfieldCounter'][$key]['countHTMLCharacters'] = $this
    ->getSetting('count_html_characters');
}