You are here

dialog_test.module in Dialog 7.2

A dummy module for testing dialog related hooks.

This is a dummy module that implements dialog related hooks to test API interaction with the Dialog module.

File

tests/dialog_test.module
View source
<?php

/**
 * @file
 * A dummy module for testing dialog related hooks.
 *
 * This is a dummy module that implements dialog related hooks to test API
 * interaction with the Dialog module.
 */

/**
 * Implements hook_menu().
 */
function dialog_test_menu() {
  $items['ajax-test/dialog'] = array(
    'title' => 'AJAX Dialog',
    'page callback' => 'dialog_test_dialog',
    'access callback' => TRUE,
  );
  $items['ajax-test/dialog-contents'] = array(
    'title' => 'AJAX Dialog contents',
    'page callback' => 'dialog_test_dialog_contents',
    'access callback' => TRUE,
  );
  $items['ajax-test/dialog-close'] = array(
    'title' => 'AJAX Dialog close',
    'page callback' => 'dialog_test_dialog_close',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Menu callback: Renders a form elements and links with #ajax['dialog'].
 */
function dialog_test_dialog() {
  drupal_add_library('dialog', 'drupal.dialog.ajax');

  // 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('dialog_test_dialog_form');

  // Dialog behavior applied to a #type => 'link'.
  $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 dialog_test_dialog_form($form, &$form_state) {
  $form['button1'] = array(
    '#type' => 'submit',
    '#name' => 'button1',
    '#value' => 'Button 1 (modal)',
    '#ajax' => array(
      'callback' => 'dialog_test_dialog_form_callback_modal',
    ),
  );
  $form['button2'] = array(
    '#type' => 'submit',
    '#name' => 'button2',
    '#value' => 'Button 2 (non-modal)',
    '#ajax' => array(
      'callback' => 'dialog_test_dialog_form_callback_nonmodal',
    ),
  );
  return $form;
}

/**
 * Non-AJAX behavior of the dialog buttons.
 */
function dialog_test_dialog_form_submit($form, &$form_state) {
  $form_state['redirect'] = 'ajax-test/dialog-contents';
}

/**
 * AJAX callback handler for dialog_test_dialog_form().
 */
function dialog_test_dialog_form_callback_modal($form, &$form_state) {
  return dialog_test_dialog_contents('ajax', TRUE);
}

/**
 * AJAX callback handler for dialog_test_dialog_form().
 */
function dialog_test_dialog_form_callback_nonmodal($form, &$form_state) {
  return dialog_test_dialog_contents('ajax', FALSE);
}

/**
 * Menu callback: Returns the contents for dialogs opened by dialog_test_dialog().
 */
function dialog_test_dialog_contents($page_mode = 'nojs', $is_modal = 0) {

  // 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',
        ),
      ),
    ),
  );
  if ($page_mode === 'ajax') {
    $commands = array(
      '#type' => 'ajax',
      '#commands' => array(),
    );
    $title = t('AJAX Dialog');
    $html = drupal_render($content);
    if ($is_modal) {
      $commands['#commands'][] = dialog_command_open_modal_dialog($title, $html);
    }
    else {
      $selector = '#ajax-test-dialog-wrapper-1';
      $commands['#commands'][] = dialog_command_open_dialog($selector, $title, $html);
    }
    return $commands;
  }
  else {
    return $content;
  }
}

/**
 * Menu callback: Close the ajax dialog.
 */
function dialog_test_dialog_close() {
  $commands = array(
    '#type' => 'ajax',
    '#commands' => array(),
  );
  $commands['#commands'][] = dialog_command_close_dialog('#ajax-test-dialog-wrapper-1');
  return $commands;
}

Functions

Namesort descending Description
dialog_test_dialog Menu callback: Renders a form elements and links with #ajax['dialog'].
dialog_test_dialog_close Menu callback: Close the ajax dialog.
dialog_test_dialog_contents Menu callback: Returns the contents for dialogs opened by dialog_test_dialog().
dialog_test_dialog_form Form builder: Renders buttons with #ajax['dialog'].
dialog_test_dialog_form_callback_modal AJAX callback handler for dialog_test_dialog_form().
dialog_test_dialog_form_callback_nonmodal AJAX callback handler for dialog_test_dialog_form().
dialog_test_dialog_form_submit Non-AJAX behavior of the dialog buttons.
dialog_test_menu Implements hook_menu().