Newer
Older
<?php
/**
* @file
* Handles the server side AJAX interactions of Views.
*/
Angie Byron
committed
use Drupal\Component\Utility\Html;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
catch
committed
use Drupal\views\Ajax\HighlightCommand;
use Drupal\Core\Ajax\OpenModalDialogCommand;
catch
committed
use Drupal\Core\Ajax\AjaxResponse;
Daniel Wehner
committed
* Wrapper around \Drupal\Core\Form\FormBuilderInterface::buildForm() to handle
* some AJAX stuff automatically.
* This makes some assumptions about the client.
*/
Dries Buytaert
committed
function views_ajax_form_wrapper($form_class, FormStateInterface &$form_state) {
if (!$form_state->has('rerender')) {
$form_state->set('rerender', FALSE);
}
Angie Byron
committed
$ajax = $form_state->get('ajax');
// Do not overwrite if the redirect has been disabled.
if (!$form_state->isRedirectDisabled()) {
Angie Byron
committed
$form_state->disableRedirect($ajax);
}
$form_state->disableCache();
$form = \Drupal::formBuilder()->buildForm($form_class, $form_state);
drupal_process_attached($form);
// These forms have the title built in, so set the title here:
Angie Byron
committed
$title = $form_state->get('title') ?: '';
if (!$ajax && $title) {
Angie Byron
committed
$form['#attached']['css'][] = drupal_get_path('module', 'views_ui') . '/css/views_ui.admin.css';
Angie Byron
committed
if ($ajax && (!$form_state->isExecuted() || $form_state->get('rerender'))) {
// If the form didn't execute and we're using ajax, build up a
// Ajax command list to execute.
catch
committed
$response = new AjaxResponse();
catch
committed
$status_messages = array('#theme' => 'status_messages');
if ($messages = drupal_render($status_messages)) {
$display = '<div class="views-messages">' . $messages . '</div>';
}
$display .= $output;
$options = array(
'dialogClass' => 'views-ui-dialog',
'width' => '50%',
);
$response->addCommand(new OpenModalDialogCommand($title, $display, $options));
Angie Byron
committed
if ($section = $form_state->get('#section')) {
$response->addCommand(new HighlightCommand('.' . Html::cleanCssIdentifier($section)));
catch
committed
return $response;
Angie Byron
committed
return $title ? ['#title' => $title, '#markup' => $output] : $output;