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.
 */

/**
 * 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/render-error'] = array(
    'title' => 'ajax_render_error',
    'page callback' => 'ajax_test_error',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['ajax-test/link'] = array(
    'title' => 'AJAX Link',
    'page callback' => 'ajax_test_link',
    'access callback' => TRUE,
  );
  $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,
  );
  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';
  return $themes;
}

/**
 * Page 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');
  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 #type link with #ajax.
 */
function ajax_test_link() {
  $build['link'] = array(
    '#type' => 'link',
    '#title' => 'Show help',
    '#href' => 'filter/tips',
    '#ajax' => array(
      'wrapper' => 'block-system-main',
    ),
  );
  return $build;
}

/**
 * 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',
    '#ajax' => array(
      'dialog' => array(
        'modal' => TRUE,
      ),
    ),
  );

  // 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',
        'ajax' => array(
          'dialog' => array(
            'modal' => TRUE,
          ),
        ),
      ),
      'link3' => array(
        'title' => 'Link 3 (non-modal)',
        'href' => 'ajax-test/dialog-contents',
        'ajax' => array(
          'dialog' => array(),
          'wrapper' => 'ajax-test-dialog-wrapper-2',
        ),
      ),
    ),
  );
  return $build;
}

/**
 * Form builder: Renders buttons with #ajax['dialog'].
 */
function ajax_test_dialog_form($form, &$form_state) {
  $form['button1'] = array(
    '#type' => 'submit',
    '#value' => 'Button 1 (modal)',
    '#ajax' => array(
      'dialog' => array(
        'modal' => TRUE,
      ),
    ),
  );
  $form['button2'] = array(
    '#type' => 'submit',
    '#value' => 'Button 2 (non-modal)',
    '#ajax' => array(
      'dialog' => array(),
      'wrapper' => 'ajax-test-dialog-wrapper-1',
    ),
  );
  return $form;
}

/**
 * Form submit handler for ajax_test_dialog_form().
 */
function ajax_test_dialog_form_submit($form, &$form_state) {
  $form_state['redirect'] = 'ajax-test/dialog-contents';
}

/**
 * Menu callback: Returns the contents for dialogs opened by ajax_test_dialog().
 */
function ajax_test_dialog_contents() {

  // This is a regular render array; the keys do not have special meaning.
  return 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',
        ),
      ),
    ),
  );
}

Functions

Namesort descending Description
ajax_test_dialog Menu callback: Renders a form elements and links with #ajax['dialog'].
ajax_test_dialog_contents Menu callback: Returns the contents for dialogs opened by ajax_test_dialog().
ajax_test_dialog_form Form builder: Renders buttons with #ajax['dialog'].
ajax_test_dialog_form_submit Form submit handler for ajax_test_dialog_form().
ajax_test_error Menu callback: Returns AJAX element with #error property set.
ajax_test_link Menu callback: Renders a #type link with #ajax.
ajax_test_menu Implements hook_menu().
ajax_test_render Page callback: Returns an element suitable for use by ajax_render().
ajax_test_system_theme_info Implements hook_system_theme_info().