ng_lightbox.module in NG Lightbox 7
Same filename and directory in other branches
The NG Lightbox module.
File
ng_lightbox.moduleView source
<?php
/**
* @file
* The NG Lightbox module.
*/
/**
* Implements hook_html_head_alter().
*/
function ng_lightbox_preprocess_html(&$vars) {
drupal_add_library('system', 'drupal.ajax');
drupal_add_js(drupal_get_path('module', 'ng_lightbox') . '/js/ng-lightbox.js', array(
'every page' => TRUE,
));
}
/**
* Implements hook_theme_registry_alter().
*/
function ng_lightbox_theme_registry_alter(&$theme_registry) {
$theme_registry['link']['module'] = drupal_get_path('module', 'ng_lightbox');
$theme_registry['link']['function'] = 'ng_lightbox_link';
}
/**
* Implements theme_link().
*/
function ng_lightbox_link($vars) {
// If this path matches the patterns, add the lightbox.
if (_ng_lightbox_check_path($vars['path'])) {
// Safety check if class isn't an array.
if (!isset($vars['options']['attributes']['class'])) {
$vars['options']['attributes']['class'] = array();
}
// Add our lightbox class.
$vars['options']['attributes']['class'][] = 'ng-lightbox';
}
// Allow other modules to alter whether this link uses the lightbox. Helpful
// if you have admin paths disabled but want to enable it for certain ones or
// vice versa.
drupal_alter('ng_lightbox_ajax_path', $vars);
return theme_link($vars);
}
/**
* Checks whether a give path matches the ng-lightbox path rules.
* This function checks both internal paths and aliased paths.
*
* @param string $path
* The path to check.
*
* @return bool
* TRUE if it matches the given rules.
*/
function _ng_lightbox_check_path($path) {
// We filter out empty paths because some modules (such as Media) use
// theme_link() to generate links with empty paths.
if (empty($path)) {
return FALSE;
}
// If we want to skip admin paths and this path is admin, return theme link.
if (variable_get('ng_lightbox_skip_admin_paths', TRUE) && path_is_admin(current_path())) {
return FALSE;
}
// Normalise the path because Drupal does the same in the router.
$path = drupal_strtolower($path);
$matches =& drupal_static(__FUNCTION__);
if (isset($matches[$path])) {
return $matches[$path];
}
// Normalise the patterns as well so they match the normalised paths.
$patterns = drupal_strtolower(variable_get('ng_lightbox_patterns', ''));
// Check for internal paths first which is much quicker than the alias lookup.
if (drupal_match_path($path, $patterns)) {
$matches[$path] = TRUE;
}
else {
// Now check for aliases paths.
$aliased_path = drupal_strtolower(drupal_get_path_alias($path));
if ($path != $aliased_path && drupal_match_path($aliased_path, $patterns)) {
$matches[$path] = TRUE;
}
else {
// No match.
$matches[$path] = FALSE;
}
}
return $matches[$path];
}
/**
* Implements hook_theme().
*/
function ng_lightbox_theme($existing, $type, $theme, $path) {
return array(
'ng_lightbox_form' => array(
'render element' => 'element',
'template' => 'templates/ng-lightbox',
),
);
}
/**
* Implements hook_permission().
*/
function ng_lightbox_permission() {
return array(
'administer ng lightbox' => array(
'title' => t('Administer NG Lightbox Paths'),
'description' => t('Perform administration tasks for NG Lightbox.'),
),
);
}
/**
* Implements hook_menu().
*/
function ng_lightbox_menu() {
$items['ng-lightbox'] = array(
'title' => '',
'page callback' => 'ng_lightbox_path_callback',
'delivery callback' => 'ajax_deliver',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['admin/config/media/ng-lightbox'] = array(
'title' => 'NG Lightbox',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ng_lightbox_settings',
),
'access arguments' => array(
'administer ng lightbox',
),
'description' => 'Configure the settings for NG Lightbox.',
'file' => 'ng_lightbox.admin.inc',
);
return $items;
}
/**
* Ajax callback for the ng_lightbox.
*/
function ng_lightbox_path_callback() {
// Currently we require a POST request, if you get here it's likely because
// something (maybe a form) accidently re-directed here.
if (!isset($_POST['ng_lightbox_path'])) {
return 'NG Lightbox error';
}
$lightbox_path = $_POST['ng_lightbox_path'];
global $base_path;
// Parse out the path parts and query string, taking into account clean urls
// and making sure to preserve query params for the next request.
$url_parts = parse_url($lightbox_path);
if (isset($url_parts['query'])) {
parse_str($url_parts['query'], $query);
$_GET = $query;
}
// If clean urls are enabled, grab the path.
if (variable_get('clean_url', 0)) {
$path = substr($url_parts['path'], strlen($base_path));
}
else {
// Clean urls are disabled so we're using the "q" param. We know we have
// $query because clean_urls are off, otherwise something went very wrong.
$path = $query['q'];
}
// Decode the path to ensure everything works with non-latin characters.
$path = urldecode($path);
// Normalize the path for Drupal.
$path = drupal_get_normal_path($path) ?: $path;
// We override these globals to make current_path() work and so that any
// forms that are rendered get the correct action set.
$_GET['q'] = $path;
$_SERVER['REQUEST_URI'] = $path;
$_SERVER['SCRIPT_NAME'] = $path;
// Execute the active menu handler.
$result = menu_execute_active_handler($path, FALSE);
// Integer results map to HTTP statuses.
if (is_int($result)) {
switch ($result) {
case MENU_NOT_FOUND:
$result = array(
'#title' => t('Page not found'),
'#markup' => t('Sorry, the content does not exist.'),
);
break;
case MENU_ACCESS_DENIED:
$result = array(
'#title' => t('Access Denied'),
'#markup' => t('Sorry, you do not have permission to view this content.'),
);
break;
case MENU_SITE_OFFLINE:
$result = array(
'#title' => t('Service unavailable'),
'#markup' => t('Sorry, the service is currently unavailable'),
);
break;
}
}
// We need to support strings inline with cores hook_menu() implementation.
if (is_string($result)) {
$html = $result;
$result = array(
'#markup' => $html,
);
}
// Setup our lightbox with some wrappers, default title and CSS.
$result['#theme_wrappers'][] = 'ng_lightbox_form';
$result['#title'] = isset($result['#title']) ? $result['#title'] : drupal_get_title();
$result['#modifier'] = isset($result['#modifier']) ? $result['#modifier'] : 'plain';
// If the default css is not disabled then add it.
if (!variable_get('ng_lightbox_disable_css', FALSE)) {
$result['#attached']['css'][] = drupal_get_path('module', 'ng_lightbox') . '/stylesheets/lightbox.css';
}
// http_build_url() would have been perfect but that lives in PECL.
$url = $url_parts['path'];
if (isset($url_parts['query'])) {
$url .= '?' . $url_parts['query'];
}
// We just set this regardless so forms have the right action.
$result['#action'] = $url;
return $result;
}
Functions
Name | Description |
---|---|
ng_lightbox_link | Implements theme_link(). |
ng_lightbox_menu | Implements hook_menu(). |
ng_lightbox_path_callback | Ajax callback for the ng_lightbox. |
ng_lightbox_permission | Implements hook_permission(). |
ng_lightbox_preprocess_html | Implements hook_html_head_alter(). |
ng_lightbox_theme | Implements hook_theme(). |
ng_lightbox_theme_registry_alter | Implements hook_theme_registry_alter(). |
_ng_lightbox_check_path | Checks whether a give path matches the ng-lightbox path rules. This function checks both internal paths and aliased paths. |