ajax_test.module

Helper module for Ajax framework tests.

File

drupal/core/modules/system/tests/modules/ajax_test/ajax_test.module
View source
<?php

/**
 * @file
 * Helper module for Ajax framework tests.
 */
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;

/**
 * Implements hook_menu().
 */
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;
}

/**
 * Implements hook_system_theme_info().
 */
function ajax_test_system_theme_info() {
  $themes['test_theme'] = drupal_get_path('module', 'system') . '/tests/themes/test_theme/test_theme.info.yml';
  return $themes;
}

/**
 * Menu callback: Returns an element suitable for use by ajax_render().
 *
 * Additionally ensures that ajax_render() incorporates JavaScript settings
 * generated during the page request by invoking drupal_add_js() with a dummy
 * setting.
 */
function ajax_test_render() {
  drupal_add_js(array(
    'ajax' => 'test',
  ), 'setting');
  $response = new AjaxResponse();
  return $response;
}

/**
 * Menu callback: Returns an AjaxResponse; settings command set last.
 *
 * Helps verifying AjaxResponse reorders commands to ensure correct execution.
 */
function ajax_test_order() {
  $response = new AjaxResponse();
  $path = drupal_get_path('module', 'system');

  // HTML insertion command.
  $response
    ->addCommand(new HtmlCommand('body', 'Hello, world!'));

  // Add two CSS files (order should remain the same).
  drupal_add_css($path . '/css/system.admin.css');
  drupal_add_css($path . '/css/system.maintenance.css');

  // Add two JavaScript files (first to the footer, should appear last).
  drupal_add_js($path . '/system.modules.js', array(
    'scope' => 'footer',
  ));
  drupal_add_js($path . '/system.js');

  // Finally, add a JavaScript setting.
  drupal_add_js(array(
    'ajax' => 'test',
  ), 'setting');
  return $response;
}

/**
 * Menu callback: Returns AJAX element with #error property set.
 */
function ajax_test_error() {
  $message = '';
  if (!empty($_GET['message'])) {
    $message = $_GET['message'];
  }
  $response = new AjaxResponse();
  $response
    ->addCommand(new AlertCommand($message));
  return $response;
}

/**
 * Menu callback: Renders a form elements and links with #ajax['dialog'].
 */
function ajax_test_dialog() {

  // Add two wrapper elements for testing non-modal dialogs. Modal dialogs use
  // the global drupal-modal wrapper by default.
  $build['dialog_wrappers'] = array(
    '#markup' => '<div id="ajax-test-dialog-wrapper-1"></div><div id="ajax-test-dialog-wrapper-2"></div>',
  );

  // Dialog behavior applied to a button.
  $build['form'] = drupal_get_form('ajax_test_dialog_form');

  // Dialog behavior applied to a #type => 'link'.
  $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',
    ),
  );

  // Dialog behavior applied to links rendered by theme_links().
  $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;
}

/**
 * Form builder: Renders buttons with #ajax['dialog'].
 */
function ajax_test_dialog_form($form, &$form_state) {

  // In order to use WebTestBase::drupalPostAJAX() to POST from a link, we need
  // to have a dummy field we can set in WebTestBase::drupalPost() else it won't
  // submit anything.
  $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;
}

/**
 * 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(TRUE);
}

/**
 * AJAX callback handler for ajax_test_dialog_form().
 */
function ajax_test_dialog_form_callback_nonmodal($form, &$form_state) {
  return _ajax_test_dialog(FALSE);
}

/**
 * Util to render dialog in ajax callback.
 *
 * @param bool $is_modal
 *   (optional) TRUE if modal, FALSE if plain dialog. Defaults to 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;
}

/**
 * Returns example content for dialog tests.
 */
function ajax_test_dialog_contents() {

  // This is a regular render array; the keys do not have special meaning.
  $content = array(
    'content' => array(
      '#markup' => 'Example message',
    ),
    'cancel' => array(
      '#type' => 'link',
      '#title' => 'Cancel',
      '#href' => '',
      '#attributes' => array(
        // This is a special class to which JavaScript assigns dialog closing
        // behavior.
        'class' => array(
          'dialog-cancel',
        ),
      ),
    ),
  );
  return $content;
}

/**
 * Menu callback: Close the ajax dialog.
 */
function ajax_test_dialog_close() {
  $response = new AjaxResponse();
  $response
    ->addCommand(new CloseDialogCommand('#ajax-test-dialog-wrapper-1'));
  return $response;
}

Functions

Namesort descending Description
ajax_test_dialog Menu callback: Renders a form elements and links with #ajax['dialog'].
ajax_test_dialog_close Menu callback: Close the ajax dialog.
ajax_test_dialog_contents Returns example content for dialog tests.
ajax_test_dialog_form Form builder: Renders buttons with #ajax['dialog'].
ajax_test_dialog_form_callback_modal AJAX callback handler for ajax_test_dialog_form().
ajax_test_dialog_form_callback_nonmodal AJAX callback handler for ajax_test_dialog_form().
ajax_test_dialog_form_submit Non-AJAX behavior of the dialog buttons.
ajax_test_error Menu callback: Returns AJAX element with #error property set.
ajax_test_menu Implements hook_menu().
ajax_test_order Menu callback: Returns an AjaxResponse; settings command set last.
ajax_test_render Menu callback: Returns an element suitable for use by ajax_render().
ajax_test_system_theme_info Implements hook_system_theme_info().
_ajax_test_dialog Util to render dialog in ajax callback.