You are here

public function TextBase::form in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/TextBase.php \Drupal\webform\Plugin\WebformElement\TextBase::form()

Gets the actual configuration webform array to be built.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An associative array contain the element's configuration webform without any default values.

Overrides WebformElementBase::form

5 calls to TextBase::form()
Hidden::form in src/Plugin/WebformElement/Hidden.php
Gets the actual configuration webform array to be built.
Telephone::form in src/Plugin/WebformElement/Telephone.php
Gets the actual configuration webform array to be built.
Textarea::form in src/Plugin/WebformElement/Textarea.php
Gets the actual configuration webform array to be built.
WebformAutocomplete::form in src/Plugin/WebformElement/WebformAutocomplete.php
Gets the actual configuration webform array to be built.
WebformEmailConfirm::form in src/Plugin/WebformElement/WebformEmailConfirm.php
Gets the actual configuration webform array to be built.
5 methods override TextBase::form()
Hidden::form in src/Plugin/WebformElement/Hidden.php
Gets the actual configuration webform array to be built.
Telephone::form in src/Plugin/WebformElement/Telephone.php
Gets the actual configuration webform array to be built.
Textarea::form in src/Plugin/WebformElement/Textarea.php
Gets the actual configuration webform array to be built.
WebformAutocomplete::form in src/Plugin/WebformElement/WebformAutocomplete.php
Gets the actual configuration webform array to be built.
WebformEmailConfirm::form in src/Plugin/WebformElement/WebformEmailConfirm.php
Gets the actual configuration webform array to be built.

File

src/Plugin/WebformElement/TextBase.php, line 148

Class

TextBase
Provides a base 'text' (field) class.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Input mask.
  $form['form']['input_mask'] = [
    '#type' => 'webform_select_other',
    '#title' => $this
      ->t('Input masks'),
    '#description' => $this
      ->t('An <a href=":href">inputmask</a> helps the user with the element by ensuring a predefined format.', [
      ':href' => 'https://github.com/RobinHerbots/jquery.inputmask',
    ]),
    '#other__option_label' => $this
      ->t('Custom…'),
    '#other__placeholder' => $this
      ->t('Enter input mask…'),
    '#other__description' => $this
      ->t('(9 = numeric; a = alphabetical; * = alphanumeric)'),
    '#empty_option' => $this
      ->t('- None -'),
    '#options' => $this
      ->getInputMaskOptions(),
  ];
  if ($this->librariesManager
    ->isExcluded('jquery.inputmask')) {
    $form['form']['input_mask']['#access'] = FALSE;
  }

  // Input hiding.
  $form['form']['input_hide'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Input hiding'),
    '#description' => $this
      ->t('Hide the input of the element when the input is not being focused.'),
    '#return_value' => TRUE,
  ];

  // Pattern.
  $form['validation']['pattern'] = [
    '#type' => 'webform_checkbox_value',
    '#title' => $this
      ->t('Pattern'),
    '#description' => $this
      ->t('A <a href=":href">regular expression</a> that the element\'s value is checked against.', [
      ':href' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions',
    ]),
    '#value__title' => $this
      ->t('Pattern regular expression'),
    '#value__description' => $this
      ->t('Enter a <a href=":href">regular expression</a> that the element\'s value should match.', [
      ':href' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions',
    ]),
    '#value__maxlength' => NULL,
  ];
  $form['validation']['pattern_error'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Pattern message'),
    '#description' => $this
      ->t('If set, this message will be used when a pattern is not matched, instead of the default "@message" message.', [
      '@message' => $this
        ->t('%name field is not in the right format.'),
    ]),
    '#states' => [
      'visible' => [
        ':input[name="properties[pattern][checkbox]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Counter.
  $form['validation'] += $this
    ->buildCounterForm();
  if (isset($form['form']['maxlength'])) {
    $form['form']['maxlength']['#description'] .= ' ' . $this
      ->t('If character counter is enabled, maxlength will automatically be set to the count maximum.');
    $form['form']['maxlength']['#states'] = [
      'invisible' => [
        ':input[name="properties[counter_type]"]' => [
          'value' => 'character',
        ],
      ],
    ];
  }
  return $form;
}