Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / settings_tray / src / Block / BlockEntitySettingTrayForm.php
index fd44b0c86aa8546264a4f6c795df473e4902b0e2..243e907ccd3ffe0663c96235dfb4cab42cb9b25b 100644 (file)
@@ -5,9 +5,9 @@ namespace Drupal\settings_tray\Block;
 use Drupal\block\BlockForm;
 use Drupal\block\BlockInterface;
 use Drupal\Component\Utility\Html;
+use Drupal\Core\Ajax\AjaxFormHelperTrait;
 use Drupal\Core\Ajax\AjaxResponse;
 use Drupal\Core\Ajax\RedirectCommand;
-use Drupal\Core\Ajax\ReplaceCommand;
 use Drupal\Core\Block\BlockPluginInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Plugin\PluginWithFormsInterface;
@@ -23,6 +23,8 @@ use Drupal\Core\Url;
  */
 class BlockEntitySettingTrayForm extends BlockForm {
 
+  use AjaxFormHelperTrait;
+
   /**
    * Provides a title callback to get the block's admin label.
    *
@@ -122,17 +124,17 @@ class BlockEntitySettingTrayForm extends BlockForm {
   public function buildForm(array $form, FormStateInterface $form_state) {
     $form = parent::buildForm($form, $form_state);
     $form['actions']['submit']['#ajax'] = [
-      'callback' => '::submitFormDialog',
+      'callback' => '::ajaxSubmit',
     ];
     $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
 
-    // static::submitFormDialog() requires data-drupal-selector to be the same
-    // between the various Ajax requests. A bug in
-    // \Drupal\Core\Form\FormBuilder prevents that from happening unless
-    // $form['#id'] is also the same. Normally, #id is set to a unique HTML ID
-    // via Html::getUniqueId(), but here we bypass that in order to work around
-    // the data-drupal-selector bug. This is okay so long as we assume that this
-    // form only ever occurs once on a page.
+    // static::ajaxSubmit() requires data-drupal-selector to be the same between
+    // the various Ajax requests. A bug in \Drupal\Core\Form\FormBuilder
+    // prevents that from happening unless $form['#id'] is also the same.
+    // Normally, #id is set to a unique HTML ID via Html::getUniqueId(), but
+    // here we bypass that in order to work around the data-drupal-selector bug.
+    // This is okay so long as we assume that this form only ever occurs once on
+    // a page.
     // @todo Remove this workaround once https://www.drupal.org/node/2897377 is
     //   fixed.
     $form['#id'] = Html::getId($form_state->getBuildInfo()['form_id']);
@@ -141,38 +143,17 @@ class BlockEntitySettingTrayForm extends BlockForm {
   }
 
   /**
-   * 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 redirects
-   *   to a URL
-   *
-   * @todo Repalce this callback with generic trait in
-   *   https://www.drupal.org/node/2896535.
+   * {@inheritdoc}
    */
-  public function submitFormDialog(array &$form, FormStateInterface $form_state) {
-    $response = new AjaxResponse();
-    if ($form_state->hasAnyErrors()) {
-      $form['status_messages'] = [
-        '#type' => 'status_messages',
-        '#weight' => -1000,
-      ];
-      $command = new ReplaceCommand('[data-drupal-selector="' . $form['#attributes']['data-drupal-selector'] . '"]', $form);
+  protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
+    if ($redirect_url = $this->getRedirectUrl()) {
+      $command = new RedirectCommand($redirect_url->setAbsolute()->toString());
     }
     else {
-      if ($redirect_url = $this->getRedirectUrl()) {
-        $command = new RedirectCommand($redirect_url->setAbsolute()->toString());
-      }
-      else {
-        // Settings Tray always provides a destination.
-        throw new \Exception("No destination provided by Settings Tray form");
-      }
+      // Settings Tray always provides a destination.
+      throw new \Exception("No destination provided by Settings Tray form");
     }
+    $response = new AjaxResponse();
     return $response->addCommand($command);
   }