You are here

public function TextBase::prepare in Webform 6.x

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

Prepare an element to be rendered within a webform.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission. Webform submission is optional since it is not used by composite sub elements.

Overrides WebformElementBase::prepare

See also

\Drupal\webform\Element\WebformCompositeBase::processWebformComposite

3 calls to TextBase::prepare()
Telephone::prepare in src/Plugin/WebformElement/Telephone.php
Prepare an element to be rendered within a webform.
TextField::prepare in src/Plugin/WebformElement/TextField.php
Prepare an element to be rendered within a webform.
WebformEmailConfirm::prepare in src/Plugin/WebformElement/WebformEmailConfirm.php
Prepare an element to be rendered within a webform.
3 methods override TextBase::prepare()
Telephone::prepare in src/Plugin/WebformElement/Telephone.php
Prepare an element to be rendered within a webform.
TextField::prepare in src/Plugin/WebformElement/TextField.php
Prepare an element to be rendered within a webform.
WebformEmailConfirm::prepare in src/Plugin/WebformElement/WebformEmailConfirm.php
Prepare an element to be rendered within a webform.

File

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

Class

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

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
  parent::prepare($element, $webform_submission);

  // Counter.
  if (!empty($element['#counter_type']) && (!empty($element['#counter_minimum']) || !empty($element['#counter_maximum'])) && $this->librariesManager
    ->isIncluded('jquery.textcounter')) {

    // Apply character min/max to min/max length.
    if ($element['#counter_type'] === 'character') {
      if (!empty($element['#counter_minimum'])) {
        $element['#minlength'] = $element['#counter_minimum'];
      }
      if (!empty($element['#counter_maximum'])) {
        $element['#maxlength'] = $element['#counter_maximum'];
      }
    }

    // Set 'data-counter-*' attributes using '#counter_*' properties.
    $data_attributes = [
      'counter_type',
      'counter_minimum',
      'counter_minimum_message',
      'counter_maximum',
      'counter_maximum_message',
    ];
    foreach ($data_attributes as $data_attribute) {
      if (empty($element['#' . $data_attribute])) {
        continue;
      }
      $data_attribute_name = 'data-' . str_replace('_', '-', $data_attribute);
      $data_attribute_value = $element['#' . $data_attribute];
      if (in_array($data_attribute, [
        'counter_minimum_message',
        'counter_maximum_message',
      ])) {
        $data_attribute_value = WebformHtmlEditor::stripTags($data_attribute_value);
      }
      $element['#attributes'][$data_attribute_name] = $data_attribute_value;
    }
    $element['#attributes']['class'][] = 'js-webform-counter';
    $element['#attributes']['class'][] = 'webform-counter';
    $element['#attached']['library'][] = 'webform/webform.element.counter';
    $element['#element_validate'][] = [
      get_class($this),
      'validateCounter',
    ];
  }

  // Input mask.
  if (!empty($element['#input_mask']) && $this->librariesManager
    ->isIncluded('jquery.inputmask')) {

    // See if the element mask is JSON by looking for 'name':, else assume it
    // is a mask pattern.
    $input_mask = $element['#input_mask'];
    if (preg_match("/^'[^']+'\\s*:/", $input_mask)) {
      $element['#attributes']['data-inputmask'] = $input_mask;
    }
    else {
      $element['#attributes']['data-inputmask-mask'] = $input_mask;
    }

    // Set input mask #pattern.
    $input_masks = $this
      ->getInputMasks();
    if (isset($input_masks[$input_mask]) && isset($input_masks[$input_mask]['pattern']) && empty($element['#pattern'])) {
      $element['#pattern'] = $input_masks[$input_mask]['pattern'];
    }
    $element['#attributes']['class'][] = 'js-webform-input-mask';
    $element['#attached']['library'][] = 'webform/webform.element.inputmask';
    $element['#element_validate'][] = [
      get_called_class(),
      'validateInputMask',
    ];
  }

  // Input hiding.
  if (!empty($element['#input_hide'])) {
    $element['#attributes']['class'][] = 'js-webform-input-hide';
    $element['#attached']['library'][] = 'webform/webform.element.inputhide';
  }

  // Pattern validation.
  // This override core's pattern validation to support unicode
  // and a custom error message.
  if (isset($element['#pattern'])) {
    $element['#attributes']['pattern'] = $element['#pattern'];
    $element['#element_validate'][] = [
      get_called_class(),
      'validatePattern',
    ];

    // Set pattern error message using #pattern_error.
    // @see Drupal.behaviors.webformRequiredError
    // @see webform.form.js
    if (!empty($element['#pattern_error'])) {
      $element['#attributes']['data-webform-pattern-error'] = WebformHtmlHelper::toPlainText($element['#pattern_error']);
    }
  }

  // Minlength attribute.
  if (isset($element['#minlength'])) {
    $element['#attributes']['minlength'] = $element['#minlength'];
  }
}