You are here

public function LayouterForm::ajaxResponse in Layouter - WYSIWYG layout templates 8

Ajax callback prints rebuilded form.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

Return value

\Drupal\Core\Ajax\AjaxResponse Returns Ajax Response.

File

src/Form/LayouterForm.php, line 135

Class

LayouterForm
Provides multistep ajax form for an layout choice.

Namespace

Drupal\layouter\Form

Code

public function ajaxResponse(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->hasAnyErrors()) {
    $form['errors']['#prefix'] = '<div class="messages messages--error">';
    $form['errors']['#suffix'] = '</div>';
    $form['errors']['#markup'] = '';
    $mess = $this
      ->messenger()
      ->messagesByType('error');
    foreach ($mess as $errors) {
      foreach ($errors as $error) {
        $form['errors']['#markup'] .= $error . '<br />';
      }
    }
    $form_state
      ->clearErrors();
  }
  $step = $form_state
    ->get('step');
  $response = new AjaxResponse();
  if ($step == $this->steps + 1 && $form_state
    ->isExecuted()) {
    $textarea_id = $form_state
      ->getBuildInfo()['args'][0];
    $content = $this
      ->buildResponseHtml($form_state);
    $command = new CloseModalDialogCommand();
    $response
      ->addCommand($command);
    $command = new InvokeCommand(NULL, 'layouterAddContent', [
      $textarea_id,
      $content,
    ]);
    $response
      ->addCommand($command);
  }
  else {
    $command = new ReplaceCommand('#layouter-form-wrapper', $form);
    $response
      ->addCommand($command);
  }
  return $response;
}