trait AjaxFormHelperTrait

Same name in other branches
  1. 9 core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php \Drupal\Core\Ajax\AjaxFormHelperTrait
  2. 10 core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php \Drupal\Core\Ajax\AjaxFormHelperTrait
  3. 11.x core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php \Drupal\Core\Ajax\AjaxFormHelperTrait

Provides a helper to for submitting an AJAX form.

@internal

Hierarchy

5 files declare their use of AjaxFormHelperTrait
BlockEntitySettingTrayForm.php in core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php
ConfigureBlockFormBase.php in core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php
ConfigureSectionForm.php in core/modules/layout_builder/src/Form/ConfigureSectionForm.php
LayoutRebuildConfirmFormBase.php in core/modules/layout_builder/src/Form/LayoutRebuildConfirmFormBase.php
MoveBlockForm.php in core/modules/layout_builder/src/Form/MoveBlockForm.php

File

core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php, line 12

Namespace

Drupal\Core\Ajax
View source
trait AjaxFormHelperTrait {
    use AjaxHelperTrait;
    
    /**
     * Submit form dialog #ajax callback.
     *
     * @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 display validation error messages or represents a
     *   successful submission.
     */
    public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
        if ($form_state->hasAnyErrors()) {
            $form['status_messages'] = [
                '#type' => 'status_messages',
                '#weight' => -1000,
            ];
            $form['#sorted'] = FALSE;
            $response = new AjaxResponse();
            $response->addCommand(new ReplaceCommand('[data-drupal-selector="' . $form['#attributes']['data-drupal-selector'] . '"]', $form));
        }
        else {
            $response = $this->successfulAjaxSubmit($form, $form_state);
        }
        return $response;
    }
    
    /**
     * Allows the form to respond to a successful AJAX submission.
     *
     * @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.
     */
    protected abstract function successfulAjaxSubmit(array $form, FormStateInterface $form_state);

}

Members

Title Sort descending Modifiers Object type Summary Overrides
AjaxFormHelperTrait::ajaxSubmit public function Submit form dialog #ajax callback.
AjaxFormHelperTrait::successfulAjaxSubmit abstract protected function Allows the form to respond to a successful AJAX submission. 5
AjaxHelperTrait::getRequestWrapperFormat protected function Gets the wrapper format of the current request.
AjaxHelperTrait::isAjax protected function Determines if the current request is via AJAX.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.