<?php
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Ajax\HtmlCommand;
function ajax_test_menu() {
$items['ajax-test/render'] = array(
'title' => 'ajax_render',
'page callback' => 'ajax_test_render',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['ajax-test/order'] = array(
'title' => 'AJAX commands order',
'page callback' => 'ajax_test_order',
'theme callback' => 'ajax_base_page_theme',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['ajax-test/render-error'] = array(
'title' => 'ajax_render_error',
'page callback' => 'ajax_test_error',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['ajax-test/dialog'] = array(
'title' => 'AJAX Dialog',
'page callback' => 'ajax_test_dialog',
'access callback' => TRUE,
);
$items['ajax-test/dialog-close'] = array(
'title' => 'AJAX Dialog close',
'page callback' => 'ajax_test_dialog_close',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_test_system_theme_info() {
$themes['test_theme'] = drupal_get_path('module', 'system') . '/tests/themes/test_theme/test_theme.info.yml';
return $themes;
}
function ajax_test_render() {
drupal_add_js(array(
'ajax' => 'test',
), 'setting');
$response = new AjaxResponse();
return $response;
}
function ajax_test_order() {
$response = new AjaxResponse();
$path = drupal_get_path('module', 'system');
$response
->addCommand(new HtmlCommand('body', 'Hello, world!'));
drupal_add_css($path . '/css/system.admin.css');
drupal_add_css($path . '/css/system.maintenance.css');
drupal_add_js($path . '/system.modules.js', array(
'scope' => 'footer',
));
drupal_add_js($path . '/system.js');
drupal_add_js(array(
'ajax' => 'test',
), 'setting');
return $response;
}
function ajax_test_error() {
$message = '';
if (!empty($_GET['message'])) {
$message = $_GET['message'];
}
$response = new AjaxResponse();
$response
->addCommand(new AlertCommand($message));
return $response;
}
function ajax_test_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_get_form('ajax_test_dialog_form');
$build['link'] = array(
'#type' => 'link',
'#title' => 'Link 1 (modal)',
'#href' => 'ajax-test/dialog-contents',
'#attributes' => array(
'class' => array(
'use-ajax',
),
'data-accepts' => 'application/vnd.drupal-modal',
),
);
$build['links'] = array(
'#theme' => 'links',
'#links' => array(
'link2' => array(
'title' => 'Link 2 (modal)',
'href' => 'ajax-test/dialog-contents',
'attributes' => array(
'class' => array(
'use-ajax',
),
'data-accepts' => 'application/vnd.drupal-modal',
'data-dialog-options' => json_encode(array(
'width' => 400,
)),
),
),
'link3' => array(
'title' => 'Link 3 (non-modal)',
'href' => 'ajax-test/dialog-contents',
'attributes' => array(
'class' => array(
'use-ajax',
),
'data-accepts' => 'application/vnd.drupal-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)',
'href' => 'ajax-test/dialog-close',
'attributes' => array(
'class' => array(
'use-ajax',
),
),
),
),
);
return $build;
}
function ajax_test_dialog_form($form, &$form_state) {
$form['textfield'] = array(
'#type' => 'hidden',
);
$form['button1'] = array(
'#type' => 'submit',
'#name' => 'button1',
'#value' => 'Button 1 (modal)',
'#ajax' => array(
'callback' => 'ajax_test_dialog_form_callback_modal',
),
);
$form['button2'] = array(
'#type' => 'submit',
'#name' => 'button2',
'#value' => 'Button 2 (non-modal)',
'#ajax' => array(
'callback' => 'ajax_test_dialog_form_callback_nonmodal',
),
);
return $form;
}
function ajax_test_dialog_form_submit($form, &$form_state) {
$form_state['redirect'] = 'ajax-test/dialog-contents';
}
function ajax_test_dialog_form_callback_modal($form, &$form_state) {
return _ajax_test_dialog(TRUE);
}
function ajax_test_dialog_form_callback_nonmodal($form, &$form_state) {
return _ajax_test_dialog(FALSE);
}
function _ajax_test_dialog($is_modal = FALSE) {
$content = ajax_test_dialog_contents();
$response = new AjaxResponse();
$title = t('AJAX Dialog contents');
$html = drupal_render($content);
if ($is_modal) {
$response
->addCommand(new OpenModalDialogCommand($title, $html));
}
else {
$selector = '#ajax-test-dialog-wrapper-1';
$response
->addCommand(new OpenDialogCommand($selector, $title, $html));
}
return $response;
}
function ajax_test_dialog_contents() {
$content = array(
'content' => array(
'#markup' => 'Example message',
),
'cancel' => array(
'#type' => 'link',
'#title' => 'Cancel',
'#href' => '',
'#attributes' => array(
'class' => array(
'dialog-cancel',
),
),
),
);
return $content;
}
function ajax_test_dialog_close() {
$response = new AjaxResponse();
$response
->addCommand(new CloseDialogCommand('#ajax-test-dialog-wrapper-1'));
return $response;
}