You are here

public function TextFieldCounterWidgetTrait::addJsPreventSubmitSettingsFormElement in Textfield Counter 8

Adds a form element to toggle JS prevention of form submission on error.

Parameters

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

bool $storageSettingMaxlengthField: Whether or not the field has storage settings that include a maximum length. Such fields allow for using the storage settings rather than the wiget setting.

5 calls to TextFieldCounterWidgetTrait::addJsPreventSubmitSettingsFormElement()
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 106

Class

TextFieldCounterWidgetTrait
Textfield counter trait. Adds textfield counting functionality.

Namespace

Drupal\textfield_counter\Plugin\Field\FieldWidget

Code

public function addJsPreventSubmitSettingsFormElement(array &$form, $storageSettingMaxlengthField = FALSE) {
  $form['js_prevent_submit'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Prevent form submission when character limit exceeded'),
    '#description' => $this
      ->t('Prevent form submission using JavaScript if the user has gone over the allowed character count.'),
    '#default_value' => $this
      ->getSetting('js_prevent_submit'),
  ];
  if ($storageSettingMaxlengthField) {
    $form['js_prevent_submit']['#states'] = [
      'invisible' => [
        [
          ':input[name="fields[' . $this->fieldDefinition
            ->getName() . '][settings_edit_form][settings][use_field_maxlength]"]' => [
            'checked' => TRUE,
          ],
        ],
        'or',
        [
          ':input[name="fields[' . $this->fieldDefinition
            ->getName() . '][settings_edit_form][settings][maxlength]"]' => [
            'value' => 0,
          ],
        ],
      ],
    ];
  }
  else {
    $form['js_prevent_submit']['#states'] = [
      'invisible' => [
        ':input[name="fields[' . $this->fieldDefinition
          ->getName() . '][settings_edit_form][settings][maxlength]"]' => [
          'value' => 0,
        ],
      ],
    ];
  }
}