View source
<?php
namespace Drupal\ajax_test\Controller;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
class AjaxTestController {
public static function dialogContents() {
$content = array(
'#title' => 'AJAX Dialog contents',
'content' => array(
'#markup' => 'Example message',
),
'cancel' => array(
'#type' => 'link',
'#title' => 'Cancel',
'#url' => Url::fromRoute('<front>'),
'#attributes' => array(
'class' => array(
'dialog-cancel',
),
),
),
);
return $content;
}
public function render() {
return [
'#attached' => [
'library' => [
'core/drupalSettings',
],
'drupalSettings' => [
'ajax' => 'test',
],
],
];
}
public function order() {
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand('body', 'Hello, world!'));
$build['#attached']['library'][] = 'ajax_test/order';
$response
->setAttachments($build['#attached']);
return $response;
}
public function renderError(Request $request) {
$message = '';
$query = $request->query;
if ($query
->has('message')) {
$message = $query
->get('message');
}
$response = new AjaxResponse();
$response
->addCommand(new AlertCommand($message));
return $response;
}
public function dialog() {
$build['dialog_wrappers'] = array(
'#markup' => '<div id="ajax-test-dialog-wrapper-1"></div><div id="ajax-test-dialog-wrapper-2"></div>',
);
$build['form'] = \Drupal::formBuilder()
->getForm('Drupal\\ajax_test\\Form\\AjaxTestDialogForm');
$build['link'] = array(
'#type' => 'link',
'#title' => 'Link 1 (modal)',
'#url' => Url::fromRoute('ajax_test.dialog_contents'),
'#attributes' => array(
'class' => array(
'use-ajax',
),
'data-dialog-type' => 'modal',
),
);
$build['links'] = array(
'#theme' => 'links',
'#links' => array(
'link2' => array(
'title' => 'Link 2 (modal)',
'url' => Url::fromRoute('ajax_test.dialog_contents'),
'attributes' => array(
'class' => array(
'use-ajax',
),
'data-dialog-type' => 'modal',
'data-dialog-options' => json_encode(array(
'width' => 400,
)),
),
),
'link3' => array(
'title' => 'Link 3 (non-modal)',
'url' => Url::fromRoute('ajax_test.dialog_contents'),
'attributes' => array(
'class' => array(
'use-ajax',
),
'data-dialog-type' => 'dialog',
'data-dialog-options' => json_encode(array(
'target' => 'ajax-test-dialog-wrapper-1',
'width' => 800,
)),
),
),
'link4' => array(
'title' => 'Link 4 (close non-modal if open)',
'url' => Url::fromRoute('ajax_test.dialog_close'),
'attributes' => array(
'class' => array(
'use-ajax',
),
'data-dialog-type' => 'modal',
),
),
'link5' => array(
'title' => 'Link 5 (form)',
'url' => Url::fromRoute('ajax_test.dialog_form'),
'attributes' => array(
'class' => array(
'use-ajax',
),
'data-dialog-type' => 'modal',
),
),
'link6' => array(
'title' => 'Link 6 (entity form)',
'url' => Url::fromRoute('contact.form_add'),
'attributes' => array(
'class' => array(
'use-ajax',
),
'data-dialog-type' => 'modal',
'data-dialog-options' => json_encode(array(
'width' => 800,
'height' => 500,
)),
),
),
'link7' => array(
'title' => 'Link 7 (non-modal, no target)',
'url' => Url::fromRoute('ajax_test.dialog_contents'),
'attributes' => array(
'class' => array(
'use-ajax',
),
'data-dialog-type' => 'dialog',
'data-dialog-options' => json_encode(array(
'width' => 800,
)),
),
),
),
);
return $build;
}
public function dialogClose() {
$response = new AjaxResponse();
$response
->addCommand(new CloseDialogCommand('#ajax-test-dialog-wrapper-1'));
return $response;
}
}