You are here

protected function ViewsFormBase::ajaxFormWrapper in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views_ui/src/Form/Ajax/ViewsFormBase.php \Drupal\views_ui\Form\Ajax\ViewsFormBase::ajaxFormWrapper()

Wrapper for handling AJAX forms.

Wrapper around \Drupal\Core\Form\FormBuilderInterface::buildForm() to handle some AJAX stuff automatically. This makes some assumptions about the client.


\Drupal\Core\Form\FormInterface|string $form_class: The value must be one of the following:

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

\Drupal\Core\Ajax\AjaxResponse|string|array Returns one of three possible values:

#markup array.

1 call to ViewsFormBase::ajaxFormWrapper()
ViewsFormBase::getForm in core/modules/views_ui/src/Form/Ajax/ViewsFormBase.php
Creates a new instance of this form.


core/modules/views_ui/src/Form/Ajax/ViewsFormBase.php, line 196


Provides a base class for Views UI AJAX forms.




protected function ajaxFormWrapper($form_class, FormStateInterface &$form_state) {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');

  // This won't override settings already in.
  if (!$form_state
    ->has('rerender')) {
      ->set('rerender', FALSE);
  $ajax = $form_state

  // Do not overwrite if the redirect has been disabled.
  if (!$form_state
    ->isRedirectDisabled()) {

  // Builds the form in a render context in order to ensure that cacheable
  // metadata is bubbled up.
  $render_context = new RenderContext();
  $callable = function () use ($form_class, &$form_state) {
    return \Drupal::formBuilder()
      ->buildForm($form_class, $form_state);
  $form = $renderer
    ->executeInRenderContext($render_context, $callable);
  if (!$render_context
    ->isEmpty()) {
  $output = $renderer

  // These forms have the title built in, so set the title here:
  $title = $form_state
    ->get('title') ?: '';
  if ($ajax && (!$form_state
    ->isExecuted() || $form_state
    ->get('rerender'))) {

    // If the form didn't execute and we're using ajax, build up an
    // Ajax command list to execute.
    $response = new AjaxResponse();

    // Attach the library necessary for using the OpenModalDialogCommand and
    // set the attachments for this Ajax response.
    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $display = '';
    $status_messages = [
      '#type' => 'status_messages',
    if ($messages = $renderer
      ->renderRoot($status_messages)) {
      $display = '<div class="views-messages">' . $messages . '</div>';
    $display .= $output;
    $options = [
      'dialogClass' => 'views-ui-dialog js-views-ui-dialog',
      'width' => '75%',
      ->addCommand(new OpenModalDialogCommand($title, $display, $options));

    // Views provides its own custom handling of AJAX form submissions.
    // Usually this happens at the same path, but custom paths may be
    // specified in $form_state.
    $form_url = $form_state
      ->has('url') ? $form_state
      ->toString() : Url::fromRoute('<current>')
      ->addCommand(new SetFormCommand($form_url));
    if ($section = $form_state
      ->get('#section')) {
        ->addCommand(new HighlightCommand('.' . Html::cleanCssIdentifier($section)));
    return $response;
  return $title ? [
    '#title' => $title,
    '#markup' => $output,
  ] : $output;