You are here

trait WebformEntityAjaxFormTrait in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Form/WebformEntityAjaxFormTrait.php \Drupal\webform\Form\WebformEntityAjaxFormTrait

Trait class for Webform Ajax support.

@todo Issue #2785047: In Outside In mode, messages should appear in the off-canvas tray, not the main page.

Hierarchy

See also

https://www.drupal.org/node/2785047

4 files declare their use of WebformEntityAjaxFormTrait
WebformDevelEntitySchemaForm.php in modules/webform_devel/src/Form/WebformDevelEntitySchemaForm.php
WebformEntityHandlersForm.php in src/WebformEntityHandlersForm.php
WebformEntityVariantsForm.php in src/WebformEntityVariantsForm.php
WebformUiEntityElementsForm.php in modules/webform_ui/src/WebformUiEntityElementsForm.php

File

src/Form/WebformEntityAjaxFormTrait.php, line 15

Namespace

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

  /**
   * {@inheritdoc}
   */
  protected function isAjax() {
    return TRUE;
  }

  /**
   * Determine if dialogs are disabled.
   *
   * @return bool
   *   TRUE if dialogs are disabled.
   */
  protected function isDialogDisabled() {
    return \Drupal::config('webform.settings')
      ->get('ui.dialog_disabled');
  }

  /**
   * Replace form via an Ajax response.
   *
   * @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 \Drupal\Core\Ajax\AjaxResponse
   *   An Ajax response that replaces a form.
   */
  protected function replaceForm(array $form, FormStateInterface $form_state) {

    // Display messages first by prefixing it the form and setting its weight
    // to -1000.
    $form = [
      'status_messages' => [
        '#type' => 'status_messages',
        '#weight' => -1000,
      ],
    ] + $form;

    // Remove wrapper.
    unset($form['#prefix'], $form['#suffix']);
    $response = $this
      ->createAjaxResponse($form, $form_state);
    $response
      ->addCommand(new WebformHtmlCommand('#' . $this
      ->getWrapperId(), $form));
    return $response;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    if ($this
      ->isDialogDisabled()) {
      return $form;
    }
    else {
      return $this
        ->buildAjaxForm($form, $form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    if (!$this
      ->getEntity()
      ->isNew() && !$this
      ->isDialogDisabled()) {
      $actions['reset'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Reset'),
        '#submit' => [
          '::noSubmit',
        ],
        '#validate' => [
          '::noSubmit',
        ],
        '#attributes' => [
          'class' => [
            'webform-ajax-refresh',
          ],
        ],
        '#weight' => 100,
      ];
    }
    return $actions;
  }

  /**
   * {@inheritdoc}
   */
  public function cancelAjaxForm(array &$form, FormStateInterface $form_state) {
    $entity = $this
      ->getEntity();
    $entity_type = $entity
      ->getEntityTypeId();
    $entity_id = $entity
      ->id();

    // Must complete reload the entity to make sure all changes are reflected.
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type);
    $entity_storage
      ->resetCache([
      $entity_id,
    ]);
    $entity = $entity_storage
      ->load($entity_id);

    // Get form object.
    $form_object = $this->entityTypeManager
      ->getFormObject($entity_type, $this->operation);

    // Set form entity.
    $form_object
      ->setEntity($entity);

    // Set form state.
    $form_state = new FormState();
    $form_state
      ->setFormState([]);
    $form_state
      ->setUserInput([]);

    // Build form.

    /** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */
    $form_builder = \Drupal::service('form_builder');
    $form = $form_builder
      ->buildForm($form_object, $form_state);

    // Return replace form as response.
    return $this
      ->replaceForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultAjaxSettings() {
    return [
      'disable-refocus' => TRUE,
      'progress' => [
        'type' => 'fullscreen',
      ],
    ];
  }

}

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::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::noSubmit public function Empty submit callback used to only have the submit button to use an #ajax submit callback. 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
WebformEntityAjaxFormTrait::actions protected function
WebformEntityAjaxFormTrait::buildForm public function 1
WebformEntityAjaxFormTrait::cancelAjaxForm public function Cancel form #ajax callback. Overrides WebformAjaxFormTrait::cancelAjaxForm
WebformEntityAjaxFormTrait::getDefaultAjaxSettings protected function Get default ajax callback settings. Overrides WebformAjaxFormTrait::getDefaultAjaxSettings
WebformEntityAjaxFormTrait::isAjax protected function Returns if webform is using Ajax. Overrides WebformAjaxFormTrait::isAjax
WebformEntityAjaxFormTrait::isDialogDisabled protected function Determine if dialogs are disabled.
WebformEntityAjaxFormTrait::replaceForm protected function Replace form via an Ajax response. Overrides WebformAjaxFormTrait::replaceForm