Skip to content
ajax.inc 2.09 KiB
Newer Older
Earl Miles's avatar
Earl Miles committed
<?php

/**
 * @file
 * Handles the server side AJAX interactions of Views.
 */

use Drupal\views\Ajax\HighlightCommand;
use Drupal\Core\Ajax\OpenModalDialogCommand;
Earl Miles's avatar
Earl Miles committed
/**
 * Wrapper around \Drupal\Core\Form\FormBuilderInterface::buildForm() to handle
 * some AJAX stuff automatically.
Earl Miles's avatar
Earl Miles committed
 * This makes some assumptions about the client.
 */
function views_ajax_form_wrapper($form_class, FormStateInterface &$form_state) {
Earl Miles's avatar
Earl Miles committed
  // This won't override settings already in.
  if (!$form_state->has('rerender')) {
    $form_state->set('rerender', FALSE);
  }
  // Do not overwrite if the redirect has been disabled.
  if (!$form_state->isRedirectDisabled()) {
  $form = \Drupal::formBuilder()->buildForm($form_class, $form_state);
Earl Miles's avatar
Earl Miles committed
  $output = drupal_render($form);
Earl Miles's avatar
Earl Miles committed

  // These forms have the title built in, so set the title here:
  $title = $form_state->get('title') ?: '';
  if (!$ajax && $title) {
    $form['#attached']['css'][] = drupal_get_path('module', 'views_ui') . '/css/views_ui.admin.css';
  if ($ajax && (!$form_state->isExecuted() || $form_state->get('rerender'))) {
Earl Miles's avatar
Earl Miles committed
    // If the form didn't execute and we're using ajax, build up a
    // Ajax command list to execute.
Earl Miles's avatar
Earl Miles committed

    $display = '';
    $status_messages = array('#theme' => 'status_messages');
    if ($messages = drupal_render($status_messages)) {
Earl Miles's avatar
Earl Miles committed
      $display = '<div class="views-messages">' . $messages . '</div>';
    }
    $display .= $output;

    $options = array(
      'dialogClass' => 'views-ui-dialog',
      'width' => '50%',
    );
    $response->addCommand(new OpenModalDialogCommand($title, $display, $options));
    if ($section = $form_state->get('#section')) {
      $response->addCommand(new HighlightCommand('.' . Html::cleanCssIdentifier($section)));
  return $title ? ['#title' => $title, '#markup' => $output] : $output;