You are here

public function BlockFormBase::submitFormAjax in Context 8.4

Same name and namespace in other branches
  1. 8 src/Reaction/Blocks/Form/BlockFormBase.php \Drupal\context\Reaction\Blocks\Form\BlockFormBase::submitFormAjax()
  2. 8.0 src/Reaction/Blocks/Form/BlockFormBase.php \Drupal\context\Reaction\Blocks\Form\BlockFormBase::submitFormAjax()

Handle when the form is submitted trough AJAX.

Return value

\Drupal\Core\Ajax\AjaxResponse An AJAX response.

File

src/Reaction/Blocks/Form/BlockFormBase.php, line 371

Class

BlockFormBase
Provides a Block Form Base for blocks reactions.

Namespace

Drupal\context\Reaction\Blocks\Form

Code

public function submitFormAjax(array &$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  if ($form_state
    ->getErrors()) {
    $messages = StatusMessages::renderMessages(NULL);
    $output[] = $messages;
    $output[] = $form;
    $form_class = '.' . str_replace('_', '-', $form_state
      ->getFormObject()
      ->getFormId());

    // Remove any previously added error messages.
    $response
      ->addCommand(new RemoveCommand('#drupal-modal .messages--error'));

    // Replace old form with new one and with error message.
    $response
      ->addCommand(new ReplaceCommand($form_class, $output));
  }
  else {
    $form = $this->contextManager
      ->getForm($this->context, 'edit');
    $response
      ->addCommand(new CloseModalDialogCommand());
    $response
      ->addCommand(new ReplaceCommand('#context-reactions', $form['reactions']));
  }
  return $response;
}