You are here

public function TextFieldCounterWidgetTrait::addMaxlengthSettingsFormElement in Textfield Counter 8

Adds a form element to set the maximum number of characters allowed.

Parameters

array $form: The form render array to which the element should be added.

bool $includeDefaultSettings: A boolean indicating whether or not to allow an override of the max length based on the default setting for the field. This should be set to true for textfields (textareas will not have a default setting for the field).

5 calls to TextFieldCounterWidgetTrait::addMaxlengthSettingsFormElement()
StringTextareaWithCounterWidget::settingsForm in src/Plugin/Field/FieldWidget/StringTextareaWithCounterWidget.php
Returns a form to configure settings for the widget.
StringTextfieldWithCounterWidget::settingsForm in src/Plugin/Field/FieldWidget/StringTextfieldWithCounterWidget.php
Returns a form to configure settings for the widget.
TextareaWithCounterWidget::settingsForm in src/Plugin/Field/FieldWidget/TextareaWithCounterWidget.php
Returns a form to configure settings for the widget.
TextareaWithSummaryAndCounterWidget::settingsForm in src/Plugin/Field/FieldWidget/TextareaWithSummaryAndCounterWidget.php
Returns a form to configure settings for the widget.
TextfieldWithCounterWidget::settingsForm in src/Plugin/Field/FieldWidget/TextfieldWithCounterWidget.php
Returns a form to configure settings for the widget.

File

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

Class

TextFieldCounterWidgetTrait
Textfield counter trait. Adds textfield counting functionality.

Namespace

Drupal\textfield_counter\Plugin\Field\FieldWidget

Code

public function addMaxlengthSettingsFormElement(array &$form, $includeDefaultSettings = FALSE) {
  if ($includeDefaultSettings) {
    $form['use_field_maxlength'] = [
      '#title' => t('Set maximum number of characters to field default (%character_count characters)', [
        '%character_count' => $this
          ->formatPlural($this
          ->getFieldSetting('max_length'), '1 character', '@count characters'),
      ]),
      '#type' => 'checkbox',
      '#default_value' => $this
        ->getSetting('use_field_maxlength'),
    ];
  }
  $form['maxlength'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum number of characters'),
    '#min' => 0,
    '#default_value' => $this
      ->getSetting('maxlength'),
    '#description' => $this
      ->t('Setting this value to zero will disable the counter on textareas.'),
  ];
  if ($includeDefaultSettings) {
    $form['maxlength']['#states']['visible'][':input[name="fields[' . $this->fieldDefinition
      ->getName() . '][settings_edit_form][settings][use_field_maxlength]"]'] = [
      'checked' => FALSE,
    ];
  }
}