Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Ajax / AjaxFormHelperTrait.php
1 <?php
2
3 namespace Drupal\Core\Ajax;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Provides a helper to for submitting an AJAX form.
9  *
10  * @internal
11  */
12 trait AjaxFormHelperTrait {
13
14   use AjaxHelperTrait;
15
16   /**
17    * Submit form dialog #ajax callback.
18    *
19    * @param array $form
20    *   An associative array containing the structure of the form.
21    * @param \Drupal\Core\Form\FormStateInterface $form_state
22    *   The current state of the form.
23    *
24    * @return \Drupal\Core\Ajax\AjaxResponse
25    *   An AJAX response that display validation error messages or represents a
26    *   successful submission.
27    */
28   public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
29     if ($form_state->hasAnyErrors()) {
30       $form['status_messages'] = [
31         '#type' => 'status_messages',
32         '#weight' => -1000,
33       ];
34       $response = new AjaxResponse();
35       $response->addCommand(new ReplaceCommand('[data-drupal-selector="' . $form['#attributes']['data-drupal-selector'] . '"]', $form));
36     }
37     else {
38       $response = $this->successfulAjaxSubmit($form, $form_state);
39     }
40     return $response;
41   }
42
43   /**
44    * Allows the form to respond to a successful AJAX submission.
45    *
46    * @param array $form
47    *   An associative array containing the structure of the form.
48    * @param \Drupal\Core\Form\FormStateInterface $form_state
49    *   The current state of the form.
50    *
51    * @return \Drupal\Core\Ajax\AjaxResponse
52    *   An AJAX response.
53    */
54   abstract protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state);
55
56 }