You are here

trait WebformDialogFormTrait in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Form/WebformDialogFormTrait.php \Drupal\webform\Form\WebformDialogFormTrait

Trait for webform dialog support.

Hierarchy

8 files declare their use of WebformDialogFormTrait
WebformDevelEntityFormApiTestForm.php in modules/webform_devel/src/Form/WebformDevelEntityFormApiTestForm.php
WebformEntityAddForm.php in src/WebformEntityAddForm.php
WebformEntityElementsForm.php in src/WebformEntityElementsForm.php
WebformSubmissionForm.php in src/WebformSubmissionForm.php
WebformSubmissionNotesForm.php in src/WebformSubmissionNotesForm.php

... See full list

File

src/Form/WebformDialogFormTrait.php, line 14

Namespace

Drupal\webform\Form
View source
trait WebformDialogFormTrait {
  use WebformAjaxFormTrait;

  /**
   * {@inheritdoc}
   */
  protected function isAjax() {
    return $this
      ->isDialog();
  }

  /**
   * Add modal dialog support to a form.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $settings
   *   Ajax settings.
   *
   * @return array
   *   The webform with modal dialog support.
   */
  protected function buildDialogForm(array &$form, FormStateInterface $form_state, array $settings = []) {
    return $this
      ->buildAjaxForm($form, $form_state, $settings);
  }

  /**
   * Add modal dialog support to a confirm form.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The webform with modal dialog support.
   */
  protected function buildDialogConfirmForm(array &$form, FormStateInterface $form_state) {
    if (!$this
      ->isDialog() || $this
      ->isOffCanvasDialog()) {
      return $form;
    }
    $this
      ->buildDialogForm($form, $form_state);

    // Replace 'Cancel' link button with a close dialog button.
    $form['actions']['cancel'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Cancel'),
      '#validate' => [
        '::noValidate',
      ],
      '#submit' => [
        '::noSubmit',
      ],
      '#weight' => 100,
      '#ajax' => [
        'callback' => '::cancelAjaxForm',
        'event' => 'click',
      ],
    ];
    return $form;
  }

  /**
   * Build webform dialog delete link.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param \Drupal\Core\Url $url
   *   The delete URL.
   */
  protected function buildDialogDeleteAction(array &$form, FormStateInterface $form_state, Url $url) {

    // WORKAROUND:
    // Create a hidden link that is clicked using jQuery.
    if ($this
      ->isDialog()) {
      $form['delete'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('Delete'),
        '#url' => $url,
        '#attributes' => [
          'style' => 'display:none',
        ] + WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, [
          'webform-dialog-delete-link',
        ]),
      ];
      $form['actions']['delete'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Delete'),
        '#attributes' => [
          'class' => [
            'button',
            'button--danger',
          ],
          'onclick' => "jQuery('.webform-dialog-delete-link').click(); return false;",
        ],
      ];
    }
    else {
      $form['actions']['delete'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('Delete'),
        '#url' => $url,
        '#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, [
          'button',
          'button--danger',
        ]),
      ];
    }
    WebformDialogHelper::attachLibraries($form);
  }

  /****************************************************************************/

  // Ajax submit callbacks.

  /****************************************************************************/

  /**
   * {@inheritdoc}
   */
  public function cancelAjaxForm(array &$form, FormStateInterface $form_state) {
    $response = $this
      ->createAjaxResponse($form, $form_state);
    $response
      ->addCommand(new CloseDialogCommand());
    return $response;
  }

  /**
   * Validate callback to clear validation errors.
   */
  public function noValidate(array &$form, FormStateInterface $form_state) {

    // Clear all validation errors.
    $form_state
      ->clearErrors();
  }

  /**
   * Empty submit callback used to only have the submit button to use an #ajax submit callback.
   */
  public function noSubmit(array &$form, FormStateInterface $form_state) {

    // Do nothing.
  }

  /**
   * Close dialog.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return bool|\Drupal\Core\Ajax\AjaxResponse
   *   An AJAX response that display validation error messages.
   */
  public function closeDialog(array &$form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $response
      ->addCommand(new CloseDialogCommand());
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WebformAjaxFormTrait::announce protected function Queue announcement with Ajax response.
WebformAjaxFormTrait::buildAjaxForm protected function Add Ajax support to a form.
WebformAjaxFormTrait::createAjaxResponse protected function Create an AjaxResponse or WebformAjaxResponse object.
WebformAjaxFormTrait::getAnnouncements protected function Get announcements.
WebformAjaxFormTrait::getDefaultAjaxSettings protected function Get default ajax callback settings. 1
WebformAjaxFormTrait::getFormStateRedirectUrl protected function Get redirect URL from the form's state.
WebformAjaxFormTrait::getWrapperId protected function Get the form's Ajax wrapper id. 1
WebformAjaxFormTrait::isCallableAjaxCallback protected function Determine if Ajax callback is callable.
WebformAjaxFormTrait::isDialog protected function Is the current request for an Ajax modal/dialog.
WebformAjaxFormTrait::isOffCanvasDialog protected function Is the current request for an off canvas dialog.
WebformAjaxFormTrait::missingAjaxCallback protected function Handle missing Ajax callback.
WebformAjaxFormTrait::replaceForm protected function Replace form via an Ajax response. 1
WebformAjaxFormTrait::resetAnnouncements protected function Reset announcements.
WebformAjaxFormTrait::setAnnouncements protected function Set announcements.
WebformAjaxFormTrait::submitAjaxForm public function Submit form #ajax callback. 1
WebformAjaxFormTrait::validateAjaxForm public function Validate form #ajax callback. 1
WebformDialogFormTrait::buildDialogConfirmForm protected function Add modal dialog support to a confirm form.
WebformDialogFormTrait::buildDialogDeleteAction protected function Build webform dialog delete link.
WebformDialogFormTrait::buildDialogForm protected function Add modal dialog support to a form.
WebformDialogFormTrait::cancelAjaxForm public function Cancel form #ajax callback. Overrides WebformAjaxFormTrait::cancelAjaxForm 1
WebformDialogFormTrait::closeDialog public function Close dialog.
WebformDialogFormTrait::isAjax protected function Returns if webform is using Ajax. Overrides WebformAjaxFormTrait::isAjax 1
WebformDialogFormTrait::noSubmit public function Empty submit callback used to only have the submit button to use an #ajax submit callback. Overrides WebformAjaxFormTrait::noSubmit
WebformDialogFormTrait::noValidate public function Validate callback to clear validation errors. 2