You are here

protected function WebformSubmissionForm::attachBehaviors in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/WebformSubmissionForm.php \Drupal\webform\WebformSubmissionForm::attachBehaviors()

Attach behaviors with libraries to the form.

Parameters

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

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

1 call to WebformSubmissionForm::attachBehaviors()
WebformSubmissionForm::form in src/WebformSubmissionForm.php
Gets the actual form array to be built.

File

src/WebformSubmissionForm.php, line 1199

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

protected function attachBehaviors(array &$form, FormStateInterface $form_state) {

  // Form: Inline form errors.
  // Add #disable_inline_form_errors property to form.
  if ($this
    ->getWebformSetting('form_disable_inline_errors')) {
    $form['#disable_inline_form_errors'] = TRUE;
  }

  // Form: Novalidate
  // Add novalidate attribute to form if client side validation disabled.
  if ($this
    ->getWebformSetting('form_novalidate')) {
    $form['#attributes']['novalidate'] = 'novalidate';
  }

  // Form: Autocomplete.
  // Add autocomplete=off attribute to form if autocompletion is disabled.
  if ($this
    ->getWebformSetting('form_disable_autocomplete')) {
    $form['#attributes']['autocomplete'] = 'off';
  }

  // Form: Disable back button.
  if ($this
    ->getWebformSetting('form_disable_back')) {
    $form['#attached']['library'][] = 'webform/webform.form.disable_back';
  }

  // Form: Back button submit.
  // Move back when back button is pressed on multistep forms.
  if ($this
    ->getWebformSetting('form_submit_back') && !$this
    ->isAjax()) {
    $form['#attached']['library'][] = 'webform/webform.form.submit_back';
  }

  // Form; Unsaved.
  // Add unsaved message.
  if ($this
    ->getWebformSetting('form_unsaved')) {
    $form['#attributes']['class'][] = 'js-webform-unsaved';

    // Set 'data-webform-unsaved' attribute if unsaved wizard.
    $pages = $this
      ->getPages($form, $form_state);
    $current_page = $this
      ->getCurrentPage($form, $form_state);
    if ($current_page && $current_page !== $this
      ->getFirstPage($pages)) {
      $form['#attributes']['data-webform-unsaved'] = TRUE;
    }
    $form['#attached']['library'][] = 'webform/webform.form.unsaved';
  }

  // Form: Submit once.
  // Prevent duplicate submissions.
  if ($this
    ->getWebformSetting('form_submit_once')) {
    $form['#attributes']['class'][] = 'js-webform-submit-once';
    $form['#attached']['library'][] = 'webform/webform.form.submit_once';
  }

  // Form: Autosubmit.
  // Disable webform auto submit on enter for wizard webform pages only.
  if ($this
    ->hasPages()) {
    $form['#attributes']['class'][] = 'js-webform-disable-autosubmit';
  }

  // Element: Autofocus.
  // Add autofocus class to webform.
  if ($this->entity
    ->isNew() && $this
    ->getWebformSetting('form_autofocus')) {
    $form['#attributes']['class'][] = 'js-webform-autofocus';
    $form['#attached']['library'][] = 'webform/webform.form.auto_focus';
  }

  // Details: Save.
  // Attach details element save open/close library.
  // This ensures that the library will be loaded even if the webform is
  // used as a block or a node.
  if ($this
    ->config('webform.settings')
    ->get('ui.details_save')) {
    $form['#attached']['library'][] = 'webform/webform.element.details.save';
  }

  // Details Toggle:
  // Display collapse/expand all details link.
  if ($this
    ->getWebformSetting('form_details_toggle')) {
    $form['#attributes']['class'][] = 'js-webform-details-toggle';
    $form['#attributes']['class'][] = 'webform-details-toggle';
    $form['#attached']['library'][] = 'webform/webform.element.details.toggle';
  }
}