modal_forms.module in Modal forms (with ctools) 7
Same filename and directory in other branches
Main file for the Modal Forms.
Modal Forms allows you to activate a Ctools based ajax modal on common Drupal forms.
File
modal_forms.moduleView source
<?php
/**
* @file
* Main file for the Modal Forms.
*
* Modal Forms allows you to activate a Ctools based ajax modal on common Drupal
* forms.
*/
/**
* Implements hook_init().
*/
function modal_forms_init() {
// Don't load modal_forms during the Drupal installation process, e.g. if part
// of installation profiles.
if (!drupal_installation_attempted()) {
_modal_forms_doheader();
}
}
/**
* Implements hook_menu().
*/
function modal_forms_menu() {
$items['admin/config/development/modal_forms'] = array(
'title' => 'Modal forms',
'description' => 'Adjust Modal forms settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'modal_forms_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'modal_forms.admin.inc',
);
$items['modal_forms/%ctools_js/login'] = array(
'title' => 'Log in',
'page callback' => 'modal_forms_login',
'page arguments' => array(
1,
),
'access callback' => 'user_is_anonymous',
'file' => 'modal_forms.pages.inc',
'type' => MENU_CALLBACK,
);
$items['modal_forms/%ctools_js/register'] = array(
'title' => 'Register',
'page callback' => 'modal_forms_register',
'page arguments' => array(
1,
),
'access callback' => 'user_register_access',
'file' => 'modal_forms.pages.inc',
'type' => MENU_CALLBACK,
);
$items['modal_forms/%ctools_js/password'] = array(
'title' => 'Password',
'page callback' => 'modal_forms_password',
'page arguments' => array(
1,
),
'access callback' => 'user_is_anonymous',
'file' => 'modal_forms.pages.inc',
'type' => MENU_CALLBACK,
);
$items['modal_forms/%ctools_js/contact'] = array(
'title' => 'Contact',
'page callback' => 'modal_forms_contact',
'page arguments' => array(
1,
),
'access arguments' => array(
'access site-wide contact form',
),
'file' => 'modal_forms.pages.inc',
'type' => MENU_CALLBACK,
);
$items['modal_forms/%ctools_js/user/%user/contact'] = array(
'title' => 'Contact',
'page callback' => 'modal_forms_personal_contact',
'page arguments' => array(
3,
1,
),
'access arguments' => array(
'access user contact forms',
),
'file' => 'modal_forms.pages.inc',
'type' => MENU_CALLBACK,
);
$items['modal_forms/%ctools_js/comment/reply/%node'] = array(
'title' => 'Comment',
'page callback' => 'modal_forms_comment_reply',
'page arguments' => array(
4,
1,
),
'access arguments' => array(
'post comments',
),
'file' => 'modal_forms.pages.inc',
'type' => MENU_CALLBACK,
);
$items['modal_forms/%ctools_js/webform/%node'] = array(
'title' => 'Webform',
'page callback' => 'modal_forms_view_webform',
'page arguments' => array(
3,
1,
),
'access callback' => 'node_access',
'access arguments' => array(
'view',
3,
),
'file' => 'modal_forms.pages.inc',
'type' => MENU_CALLBACK,
);
$items['modal_forms/%ctools_js/dismiss'] = array(
'title' => 'Dismiss',
'page callback' => 'modal_forms_dismiss',
'page arguments' => array(
1,
),
// JS to ajax_render.
'access callback' => TRUE,
'file' => 'modal_forms.pages.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_form_alter().
*/
function modal_forms_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_login':
if (arg(0) == 'modal_forms') {
$form['name']['#size'] = 25;
$form['pass']['#size'] = 25;
// Add links as needed.
if (variable_get('modal_forms_login_links', 0)) {
$items = array();
// Add standard links.
if (variable_get('modal_forms_login_links', 0) == 1) {
if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
$items[] = l(t('Create new account'), 'user/register', array(
'attributes' => array(
'title' => t('Create a new user account.'),
),
));
}
$items[] = l(t('Request new password'), 'user/password', array(
'attributes' => array(
'title' => t('Request new password via e-mail.'),
),
));
}
// Add links that opens in a modal_forms.
if (variable_get('modal_forms_login_links', 0) == 2) {
if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
$items[] = ctools_modal_text_button(t('Create new account'), 'modal_forms/nojs/register', t('Create a new user account'), 'ctools-modal-modal-popup-medium');
}
$items[] = ctools_modal_text_button(t('Request new password'), 'modal_forms/nojs/password', t('Request new password via e-mail.'), 'ctools-modal-modal-popup-small');
}
$form['links'] = array(
'#markup' => theme('item_list', array(
'items' => $items,
)),
);
}
}
break;
case 'user_register_form':
if (arg(0) == 'modal_forms') {
$form['account']['name']['#size'] = 30;
$form['account']['mail']['#size'] = 30;
}
break;
case 'user_pass':
if (arg(0) == 'modal_forms') {
$form['name']['#size'] = 30;
}
break;
}
}
/**
* Implements hook_form_BASE_FROM_ID_alter().
*/
function modal_forms_form_comment_form_alter(&$form, &$form_state, $form_id) {
if (variable_get('modal_forms_comment', 0)) {
$comment = $form_state['comment'];
// If not replying to a comment, use our dedicated page callback for new
// comments on nodes.
if (empty($comment->cid) && empty($comment->pid)) {
// Change the action to call our function.
$form['#action'] = url('modal_forms/nojs/comment/reply/' . $comment->nid);
}
}
}
/**
* Check if modal_forms should be active for the current URL.
*
* @return bool
* TRUE if modal_forms should be active for the current page.
*/
function _modal_forms_active() {
global $is_https;
// Optionally disable modal activation for insecure/non-https pages.
if (!$is_https && variable_get('modal_forms_ssl', 0)) {
return FALSE;
}
// Code from the block_list funtion in block.module.
$path = drupal_get_path_alias($_GET['q']);
$modal_forms_pages = variable_get('modal_forms_pages', "admin*\nimagebrowser*\nimg_assist*\nimce*\nnode/add/*\nnode/*/edit\nprint/*\nprintpdf/*\nsystem/ajax\nsystem/ajax/*");
// Compare with the internal and path alias (if any).
$page_match = drupal_match_path($path, $modal_forms_pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $modal_forms_pages);
}
$page_match = variable_get('modal_forms_visibility', 0) == 0 ? !$page_match : $page_match;
return $page_match;
}
/**
* Loads the various js and css files.
*/
function _modal_forms_doheader() {
static $already_added = FALSE;
if ($already_added) {
// Don't add the JavaScript and CSS multiple times.
return;
}
if (!_modal_forms_active()) {
// Don't add the JavaScript and CSS on specified paths.
return;
}
// Include the CTools tools that we need.
ctools_include('ajax');
ctools_include('modal');
// Add CTools' javascript to the page.
ctools_modal_add_js();
$throbber = theme('image', array(
'path' => ctools_image_path('loading_animation.gif', 'modal_forms'),
'alt' => t('Loading...'),
'title' => t('Loading'),
));
$js_settings = array(
'modal-popup-small' => array(
'modalSize' => array(
'type' => variable_get('modal_forms_popup_small_type', 'fixed'),
'width' => floatval(variable_get('modal_forms_popup_small_width', 300)),
'height' => floatval(variable_get('modal_forms_popup_small_height', 300)),
),
'modalOptions' => array(
'opacity' => floatval(variable_get('modal_forms_opacity', 0.85)),
'background-color' => variable_get('modal_forms_background_color', '#000'),
),
'animation' => 'fadeIn',
'modalTheme' => 'ModalFormsPopup',
'throbber' => $throbber,
'closeText' => t('Close'),
),
'modal-popup-medium' => array(
'modalSize' => array(
'type' => variable_get('modal_forms_popup_medium_type', 'fixed'),
'width' => floatval(variable_get('modal_forms_popup_medium_width', 550)),
'height' => floatval(variable_get('modal_forms_popup_medium_height', 450)),
),
'modalOptions' => array(
'opacity' => floatval(variable_get('modal_forms_opacity', 0.85)),
'background-color' => variable_get('modal_forms_background_color', '#000'),
),
'animation' => 'fadeIn',
'modalTheme' => 'ModalFormsPopup',
'throbber' => $throbber,
'closeText' => t('Close'),
),
'modal-popup-large' => array(
'modalSize' => array(
'type' => variable_get('modal_forms_popup_large_type', 'scale'),
'width' => floatval(variable_get('modal_forms_popup_large_width', 0.8)),
'height' => floatval(variable_get('modal_forms_popup_large_height', 0.8)),
),
'modalOptions' => array(
'opacity' => floatval(variable_get('modal_forms_opacity', 0.85)),
'background-color' => variable_get('modal_forms_background_color', '#000'),
),
'animation' => 'fadeIn',
'modalTheme' => 'ModalFormsPopup',
'throbber' => $throbber,
'closeText' => t('Close'),
),
);
// Allow other modules to change or add modal javascript settings.
drupal_alter('modal_forms_js_settings', $js_settings);
drupal_add_js($js_settings, 'setting');
// Add modal_forms own js and CSS.
ctools_add_css('modal_forms_popup', 'modal_forms');
ctools_add_js('modal_forms_popup', 'modal_forms');
$path = drupal_get_path('module', 'modal_forms');
// Automatically rewrite selected links to open in a modal.
if ($GLOBALS['user']->uid == 0) {
if (variable_get('modal_forms_login', 0)) {
drupal_add_js($path . '/js/modal_forms_login.js', array(
'weight' => -20,
));
}
if (variable_get('modal_forms_register', 0)) {
drupal_add_js($path . '/js/modal_forms_register.js', array(
'weight' => -20,
));
}
if (variable_get('modal_forms_password', 0)) {
drupal_add_js($path . '/js/modal_forms_password.js', array(
'weight' => -20,
));
}
}
if (variable_get('modal_forms_contact', 0)) {
drupal_add_js($path . '/js/modal_forms_contact.js', array(
'weight' => -20,
));
}
if (user_access('post comments') && variable_get('modal_forms_comment', 0)) {
drupal_add_js($path . '/js/modal_forms_comment.js', array(
'weight' => -20,
));
}
$already_added = TRUE;
}
/**
* Implements hook_help().
*/
function modal_forms_help($path) {
switch ($path) {
case 'admin/help#modal_forms':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Modal forms make use of the modal feature in the ctools module to open some common forms in a modal window.') . '</p>';
$output .= '<h3>' . t('Supported forms') . '</h3>';
$output .= '<ul>';
$output .= '<li>' . t('Log in (modal_forms/nojs/login)') . '</li>';
$output .= '<li>' . t('Request new password (modal_forms/nojs/password)') . '</li>';
$output .= '<li>' . t('Create new account (modal_forms/nojs/register)') . '</li>';
$output .= '<li>' . t('Contact (modal_forms/nojs/contact)') . '</li>';
$output .= '<li>' . t('Comment (modal_forms/nojs/comment/reply/%node)') . '</li>';
$output .= '<li>' . t('Webform (modal_forms/nojs/webform/%node)') . '</li>';
$output .= '</ul>';
$output .= '<p>' . t('Function that rewrite normal links to modal forms links (except for webforms) can be activated on the project configuration page.') . '</p>';
$output .= '<h3>' . t('Links to the modal version of the forms') . '</h3>';
$output .= '<p>' . t('JavaScript that can rewrite standard links to these forms in to a modal equivalent are provided, see the project configuration page to activate them.') . '</p>';
$output .= '<p>' . t('To build links in code I recommend using the ctools_modal_text_button() function.') . '</p>';
$output .= '<code>';
$output .= '<p>' . '$links[] = ' . "ctools_modal_text_button(t('Modal Login'), 'modal_forms/nojs/login', t('Login via modal'));" . '</p>';
$output .= '<p>' . '$links[] = ' . "ctools_modal_text_button(t('Modal Login'), 'modal_forms/nojs/login', t('Login via modal'), 'ctools-modal-modal-popup-small');" . '</p>';
$output .= '</code>';
$output .= '<p>' . t('It is also possible to build links manually.') . '</p>';
$output .= '<code>';
$output .= '<p>' . '<a class="ctools-use-modal" href="modal_forms/nojs/login">Modal Login</a>' . '</p>';
$output .= '<p>' . '<a class="ctools-use-modal ctools-modal-modal-popup-small" href="modal_forms/nojs/login">Modal Login</a>' . '</p>';
$output .= '</code>';
$output .= '<h3>' . t('Popup styles and sizes') . '</h3>';
$output .= '<p>' . t('The include CSS file tries to mimic the default Colorbox style.') . '</p>';
$output .= '<p>' . t('The extra class "ctools-modal-modal-popup-small" in the links above tells the module what popup style size to use. There are three options included.') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('ctools-modal-modal-popup-small (defaults to 300x300)') . '</li>';
$output .= '<li>' . t('ctools-modal-modal-popup-medium (defaults to 550x450)') . '</li>';
$output .= '<li>' . t('ctools-modal-modal-popup-large (defaults to 80%x80%)') . '</li>';
$output .= '</ul>';
$output .= '<p>' . t('Go to the configurations page for Modal forms to adjust the sizes.') . '</p>';
return $output;
}
}
Functions
Name![]() |
Description |
---|---|
modal_forms_form_alter | Implements hook_form_alter(). |
modal_forms_form_comment_form_alter | Implements hook_form_BASE_FROM_ID_alter(). |
modal_forms_help | Implements hook_help(). |
modal_forms_init | Implements hook_init(). |
modal_forms_menu | Implements hook_menu(). |
_modal_forms_active | Check if modal_forms should be active for the current URL. |
_modal_forms_doheader | Loads the various js and css files. |