- <?php
- * @file
- * Helper module for Ajax framework tests.
- */
-
- * Implements hook_menu().
- */
- function ajax_test_menu() {
- $items['ajax-test/render'] = array(
- 'title' => 'ajax_render',
- 'page callback' => 'ajax_test_render',
- 'delivery callback' => 'ajax_deliver',
- 'access callback' => TRUE,
- 'type' => MENU_CALLBACK,
- );
- $items['ajax-test/render-error'] = array(
- 'title' => 'ajax_render_error',
- 'page callback' => 'ajax_test_error',
- 'delivery callback' => 'ajax_deliver',
- '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-contents'] = array(
- 'title' => 'AJAX Dialog contents',
- 'page callback' => 'ajax_test_dialog_contents',
- '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;
- }
-
- * Implements hook_system_theme_info().
- */
- function ajax_test_system_theme_info() {
- $themes['test_theme'] = backdrop_get_path('module', 'ajax_test') . '/themes/test_theme/test_theme.info';
- return $themes;
- }
-
- * Menu callback; Return an element suitable for use by ajax_deliver().
- *
- * Additionally ensures that ajax_render() incorporates JavaScript settings
- * generated during the page request by invoking backdrop_add_js() with a dummy
- * setting.
- */
- function ajax_test_render() {
- backdrop_add_js(array('ajax' => 'test'), 'setting');
- return array('#type' => 'ajax', '#commands' => array());
- }
-
- * Menu callback; Returns Ajax element with #error property set.
- */
- function ajax_test_error() {
- $message = '';
- if (!empty($_GET['message'])) {
- $message = $_GET['message'];
- }
- return array('#type' => 'ajax', '#error' => $message);
- }
-
- * Menu callback: Renders a form elements and links with #ajax['dialog'].
- */
- function ajax_test_dialog() {
- backdrop_add_library('system', 'backdrop.ajax');
-
-
-
- $build['dialog_wrappers'] = array('#markup' => '<div id="ajax-test-dialog-wrapper-1"></div><div id="ajax-test-dialog-wrapper-2"></div>');
-
-
- $build['form'] = backdrop_get_form('ajax_test_dialog_form');
-
-
- $build['client_side_title']['#markup'] = '<h2>Client-side dialog links</h2><p>These links have no special server-side handling. They request a normal page as a dialog. They use the "use-ajax" class combined with the "data-dialog" and "data-dialog-options" properties.</p>';
- $build['client_side_links'] = array(
- '#theme' => 'links',
- '#links' => array(
- 'link1' => array(
- 'title' => 'Link 1 (modal)',
- 'href' => 'ajax-test/dialog-contents/0/1',
- 'attributes' => array(
- 'class' => array('use-ajax'),
- 'data-dialog' => 'true',
- ),
- ),
- 'link2' => array(
- 'title' => 'Link 2 (non-modal)',
- 'href' => 'ajax-test/dialog-contents/0/1',
- 'attributes' => array(
- 'class' => array('use-ajax'),
- 'data-dialog' => 'true',
- 'data-dialog-options' => json_encode(array('target' => '#ajax-test-dialog-wrapper-1')),
- ),
- ),
- ),
- );
-
- $build['server_side_title']['#markup'] = '<h2>Server-side dialog links</h2><p>These links only have the "use-ajax" class and all dialog handling is server-side.</p>';
- $build['server_side_links'] = array(
- '#theme' => 'links',
- '#links' => array(
- 'link2' => array(
- 'title' => 'Link 1 (modal)',
- 'href' => 'ajax-test/dialog-contents/nojs/1',
- 'attributes' => array('class' => array('use-ajax')),
- ),
- 'link3' => array(
- 'title' => 'Link 2 (non-modal)',
- 'href' => 'ajax-test/dialog-contents/nojs',
- 'attributes' => array('class' => array('use-ajax')),
- ),
- 'link4' => array(
- 'title' => 'Link 3 (close non-modal if open)',
- 'href' => 'ajax-test/dialog-close',
- 'attributes' => array('class' => array('use-ajax')),
- ),
- ),
- );
- return $build;
- }
-
- * Form builder: Renders buttons with #ajax['dialog'].
- */
- function ajax_test_dialog_form($form, &$form_state) {
- $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;
- }
-
- * Non-AJAX behavior of the dialog buttons.
- */
- function ajax_test_dialog_form_submit($form, &$form_state) {
- $form_state['redirect'] = 'ajax-test/dialog-contents';
- }
-
- * AJAX callback handler for ajax_test_dialog_form().
- */
- function ajax_test_dialog_form_callback_modal($form, &$form_state) {
- return ajax_test_dialog_contents('ajax', TRUE);
- }
-
- * AJAX callback handler for ajax_test_dialog_form().
- */
- function ajax_test_dialog_form_callback_nonmodal($form, &$form_state) {
- return ajax_test_dialog_contents('ajax', FALSE);
- }
-
- * Menu callback: Returns the contents for dialogs opened by ajax_test_dialog().
- */
- function ajax_test_dialog_contents($page_mode = 'nojs', $is_modal = 0) {
-
- $content = array(
- 'content' => array(
- '#markup' => 'Example message',
- ),
- 'cancel'=> array(
- '#type' => 'link',
- '#title' => 'Cancel',
- '#href' => '',
- '#attributes' => array(
-
-
- 'class' => array('dialog-cancel'),
- ),
- ),
- );
-
- if ($page_mode === 'ajax') {
- $commands = array(
- '#type' => 'ajax',
- '#commands' => array(),
- );
- $title = t('AJAX Dialog');
- $html = backdrop_render($content);
- if ($is_modal) {
- $commands['#commands'][] = ajax_command_open_modal_dialog($title, $html);
- }
- else {
- $selector = '#ajax-test-dialog-wrapper-1';
- $commands['#commands'][] = ajax_command_open_dialog($selector, $title, $html);
- }
- return $commands;
- }
- else {
- return $content;
- }
- }
-
- * Menu callback: Close the ajax dialog.
- */
- function ajax_test_dialog_close() {
- $commands = array(
- '#type' => 'ajax',
- '#commands' => array(),
- );
- $commands['#commands'][] = ajax_command_close_dialog('#ajax-test-dialog-wrapper-1');
- return $commands;
- }