dialog.module in Dialog 6
Same filename and directory in other branches
File
dialog.moduleView source
<?php
/**
* Implementation of hook_theme().
*/
function dialog_theme() {
return array(
'dialog_content' => array(
'arguments' => array(
'content' => NULL,
),
'template' => 'dialog-content',
),
);
}
/**
* Implementation of hook_menu().
*/
function dialog_menu() {
$items = array();
$items['admin/settings/dialog'] = array(
'title' => 'Dialog API',
'description' => 'Set default attributes for dialogs',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'dialog_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'dialog.admin.inc',
);
return $items;
}
/**
* Add all the necessary javascript (and css) to be able to display a dialog
* on the current page. This must be used on any page that could possibly
* contain a dialog. It is safe to call this function repeatedly.
*/
function dialog_add_js() {
// Provide a gate so we only do this once.
static $done = FALSE;
if ($done) {
return;
}
// Include CTools ajax so it can preprocess the page's CSS and JS. Helps avoid
// issues with cache clears.
ctools_include('ajax');
$settings = array(
'Dialog' => array(
'throbber' => theme('image', ctools_image_path('throbber.gif'), t('Loading...'), t('Loading')),
'height' => variable_get('dialog_default_height', 'auto'),
'width' => variable_get('dialog_default_width', '600px'),
),
);
drupal_add_js($settings, 'setting');
drupal_add_js('misc/jquery.form.js');
ctools_add_js('ajax-responder');
// Add jquery_ui js and css.
jquery_ui_add(array(
'ui.core',
'ui.resizable',
'ui.draggable',
'ui.dialog',
));
// Get the correct CSS path based on jQuery UI version.
$version_16 = version_compare(jquery_ui_get_version(), '1.7.0', '<');
$css_path = $version_16 ? 'default' : 'base';
drupal_add_css(JQUERY_UI_PATH . '/themes/' . $css_path . '/ui.all.css');
// And finally, the dialog js.
drupal_add_js(drupal_get_path('module', 'dialog') . '/dialog.js');
// Close the gate.
$done = TRUE;
}
/**
* Place HTML within the modal.
*
* @param $title
* The title of the modal.
* @param $html
* The html to place within the modal.
*/
function dialog_command_display($title, $html, $options = array()) {
return array(
'command' => 'dialog_display',
'title' => $title,
'output' => theme('dialog_content', $html),
'options' => $options,
);
}
/**
* Dismiss the modal.
*/
function dialog_command_dismiss() {
return array(
'command' => 'dialog_dismiss',
);
}
/**
* Display loading screen in the modal
*/
function dialog_command_loading() {
return array(
'command' => 'dialog_loading',
);
}
/**
* Perform a simple modal render and immediately exit.
*
* This is primarily used for error displays, since usually modals will
* contain forms.
*/
function dialog_ajax_render($title, $output, $options = array()) {
ctools_include('ajax');
$commands = array();
$commands[] = dialog_command_display($title, $output, $options);
ctools_ajax_render($commands);
}
/**
* Wrap a form so that we can use it properly with AJAX. Essentially if the
* form wishes to render, it automatically does that, otherwise it returns
* so we can see submission results.
*
* @return
* The output of the form, if it was rendered. If $form_state['ajax']
* is set, this will use ctools_modal_form_render so it will be
* a $command object suitable for ctools_ajax_render already.
*
* The return will be NULL if the form was successfully submitted unless
* you specifically set re_render = TRUE. If ajax is set the
* form will never be redirected.
*/
function dialog_form_wrapper($form_id, &$form_state) {
ctools_include('form');
// This won't override settings already in.
$form_state += array(
're_render' => FALSE,
'no_redirect' => !empty($form_state['ajax']),
);
$output = ctools_build_form($form_id, $form_state);
if (!empty($form_state['ajax']) && empty($form_state['executed'])) {
return dialog_form_render($form_state, $output);
}
return $output;
}
/**
* Render a form into an AJAX display.
*/
function dialog_form_render($form_state, $output) {
$title = empty($form_state['title']) ? '' : $form_state['title'];
// If there are messages for the form, render them.
if ($messages = theme('status_messages')) {
$output = $messages . $output;
}
$commands = array();
if (isset($form_state['js settings'])) {
$commands[] = ctools_ajax_command_settings($form_state['js settings']);
}
$commands[] = dialog_command_display($title, $output);
return $commands;
}
/**
* Generic dialog replacement for drupal_get_form(). Suitable for use as the
* page callback in a menu item.
*
* This function introduces a new form callback function to handle the
* post-submit dialog commands, in the ajax context. This function takes the
* form of form_id_dialog_success. If this function is found, it will be
* automatically called after a valid submission of the form has been detected.
* If the function does not exist, a redirect will be issued based on the
* redirect value in the form_state array. As the final fallback, if the
* redirect value is missing or empty, a client-side reload command is issued.
*
* @param $id
* The form_id that would normally be passed to drupal_get_form.
* @param $js
* The %ctools_js wildcard parameter to specify when the call is being made
* in a javascript context.
* @param ...
* Any additional parameters will be passed on to the form builder function.
*/
function dialog_get_form($form_id, $js) {
$args = func_get_args();
$form_id = array_shift($args);
$js = array_shift($args);
$form_state = array(
'ajax' => (bool) $js,
'title' => drupal_get_title(),
'args' => $args,
);
$output = dialog_form_wrapper($form_id, $form_state);
if ($js) {
ctools_include('ajax');
if (empty($output)) {
$func = $form_id . '_dialog_success';
if (function_exists($func)) {
$output = $func($form_state);
}
else {
if (!empty($form_state['redirect'])) {
$output[] = ctools_ajax_command_redirect($form_state['redirect']);
}
else {
$output[] = ctools_ajax_command_reload();
}
}
}
ctools_ajax_render($output);
}
else {
return $output;
}
}
/**
* Process variables for dialog-content.tpl.php.
*/
function template_preprocess_dialog_content(&$variables) {
$variables['help'] = theme('help');
$variables['messages'] = theme('status_messages');
}
Functions
Name | Description |
---|---|
dialog_add_js | Add all the necessary javascript (and css) to be able to display a dialog on the current page. This must be used on any page that could possibly contain a dialog. It is safe to call this function repeatedly. |
dialog_ajax_render | Perform a simple modal render and immediately exit. |
dialog_command_dismiss | Dismiss the modal. |
dialog_command_display | Place HTML within the modal. |
dialog_command_loading | Display loading screen in the modal |
dialog_form_render | Render a form into an AJAX display. |
dialog_form_wrapper | Wrap a form so that we can use it properly with AJAX. Essentially if the form wishes to render, it automatically does that, otherwise it returns so we can see submission results. |
dialog_get_form | Generic dialog replacement for drupal_get_form(). Suitable for use as the page callback in a menu item. |
dialog_menu | Implementation of hook_menu(). |
dialog_theme | Implementation of hook_theme(). |
template_preprocess_dialog_content | Process variables for dialog-content.tpl.php. |