drupal/core/modules/views/includes/ajax.inc

69 lines
2.1 KiB
PHP
Raw Normal View History

2009-05-17 11:16:51 +00:00
<?php
/**
* @file
* Handles the server side AJAX interactions of Views.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Ajax\HighlightCommand;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\AjaxResponse;
2009-05-17 11:16:51 +00:00
/**
* Wrapper around \Drupal\Core\Form\FormBuilderInterface::buildForm() to handle
* some AJAX stuff automatically.
2009-05-17 11:16:51 +00:00
* This makes some assumptions about the client.
*/
function views_ajax_form_wrapper($form_class, FormStateInterface &$form_state) {
2009-05-17 11:16:51 +00:00
// This won't override settings already in.
if (!$form_state->has('rerender')) {
$form_state->set('rerender', FALSE);
}
$ajax = $form_state->get('ajax');
// Do not overwrite if the redirect has been disabled.
if (!$form_state->isRedirectDisabled()) {
$form_state->disableRedirect($ajax);
}
$form_state->disableCache();
2009-05-17 11:16:51 +00:00
$form = \Drupal::formBuilder()->buildForm($form_class, $form_state);
2009-05-17 11:16:51 +00:00
$output = drupal_render($form);
drupal_process_attached($form);
2009-05-17 11:16:51 +00:00
// 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';
2009-05-17 11:16:51 +00:00
}
if ($ajax && (!$form_state->isExecuted() || $form_state->get('rerender'))) {
2009-05-17 11:16:51 +00:00
// If the form didn't execute and we're using ajax, build up a
// Ajax command list to execute.
$response = new AjaxResponse();
2009-05-17 11:16:51 +00:00
$display = '';
$status_messages = array('#theme' => 'status_messages');
if ($messages = drupal_render($status_messages)) {
2009-05-17 11:16:51 +00:00
$display = '<div class="views-messages">' . $messages . '</div>';
}
$display .= $output;
$options = array(
'dialogClass' => 'views-ui-dialog',
'width' => '50%',
);
2009-05-17 11:16:51 +00:00
$response->addCommand(new OpenModalDialogCommand($title, $display, $options));
2009-05-17 11:16:51 +00:00
if ($section = $form_state->get('#section')) {
$response->addCommand(new HighlightCommand('.' . Html::cleanCssIdentifier($section)));
2009-05-17 11:16:51 +00:00
}
return $response;
2009-05-17 11:16:51 +00:00
}
return $title ? ['#title' => $title, '#markup' => $output] : $output;
2009-05-17 11:16:51 +00:00
}