Commit 238bb433 authored by frjo's avatar frjo
Browse files

Made Libraries API 2.x a dependency and use it to find and load the Colorbox...

Made Libraries API 2.x a dependency and use it to find and load the Colorbox plugin. Saves some 30 lines of code.
parent 6eb1578b
......@@ -11,7 +11,7 @@
function colorbox_admin_settings() {
drupal_add_js(drupal_get_path('module', 'colorbox') . '/js/colorbox_admin_settings.js', array('preprocess' => FALSE));
$colorbox_path = colorbox_get_path();
$library = libraries_detect('colorbox');
$form['colorbox_plugin_settings'] = array(
'#type' => 'fieldset',
......@@ -19,21 +19,14 @@ function colorbox_admin_settings() {
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['colorbox_plugin_settings']['colorbox_path'] = array(
'#type' => 'textfield',
'#title' => t('Path'),
'#default_value' => $colorbox_path,
'#description' => t('The location where Colorbox plugin is installed. Relative paths are from the Drupal root directory.'),
'#after_build' => array('_colorbox_admin_settings_check_plugin_path'),
);
$form['colorbox_plugin_settings']['colorbox_compression_type'] = array(
'#type' => 'radios',
'#title' => t('Choose Colorbox compression level'),
'#options' => array(
'min' => t('Production (Minified)'),
'none' => t('Development (Uncompressed Code)'),
'minified' => t('Production (Minified)'),
'source' => t('Development (Uncompressed Code)'),
),
'#default_value' => variable_get('colorbox_compression_type', 'min'),
'#default_value' => variable_get('colorbox_compression_type', 'minified'),
);
if (module_exists('insert')) {
......@@ -83,11 +76,11 @@ function colorbox_admin_settings() {
$colorbox_styles = array(
'default' => t('Default'),
'stockholmsyndrome' => t('Stockholm Syndrome'),
$colorbox_path . '/example1' => t('Example 1'),
$colorbox_path . '/example2' => t('Example 2'),
$colorbox_path . '/example3' => t('Example 3'),
$colorbox_path . '/example4' => t('Example 4'),
$colorbox_path . '/example5' => t('Example 5'),
$library['library path'] . '/example1' => t('Example 1'),
$library['library path'] . '/example2' => t('Example 2'),
$library['library path'] . '/example3' => t('Example 3'),
$library['library path'] . '/example4' => t('Example 4'),
$library['library path'] . '/example5' => t('Example 5'),
'none' => t('None'),
);
$form['colorbox_custom_settings']['colorbox_style'] = array(
......@@ -305,20 +298,3 @@ function colorbox_admin_settings() {
return system_settings_form($form);
}
/**
* Checks if the directory in $form_element exists and contains a file named
* 'jquery.colorbox.js'. If validation fails, the form element is flagged
* with an error from within the file_check_directory function.
*
* @param $form_element
* The form element containing the name of the directory to check.
*/
function _colorbox_admin_settings_check_plugin_path($form_element) {
$library_path = $form_element['#value'];
if (!is_dir($library_path) || !(file_exists($library_path . '/colorbox/jquery.colorbox.js') && file_exists($library_path . '/colorbox/jquery.colorbox-min.js'))) {
form_set_error($form_element['#parents'][0], t('You need to download the !colorbox and extract the entire contents of the archive into the %path folder of your server.', array('!colorbox' => l(t('Colorbox plugin'), 'http://colorpowered.com/colorbox/'), '%path' => $library_path)));
}
return $form_element;
}
name = Colorbox
description = A light-weight, customizable lightbox plugin for jQuery 1.4.3+.
dependencies[] = libraries (2.x)
core = 7.x
configure = admin/config/media/colorbox
......
......@@ -13,14 +13,21 @@ function colorbox_requirements($phase) {
if ($phase == 'runtime') {
$t = get_t();
$library_path = colorbox_get_path();
$colorbox_version = colorbox_get_version();
$library = libraries_detect('colorbox');
if (version_compare($colorbox_version, COLORBOX_MIN_PLUGIN_VERSION, '>=')) {
if (empty($library['installed'])) {
$requirements['colorbox_plugin'] = array(
'title' => $t('Colorbox plugin'),
'value' => $t('At least @a', array('@a' => COLORBOX_MIN_PLUGIN_VERSION)),
'severity' => REQUIREMENT_ERROR,
'description' => $t('You need to download the !colorbox and extract the entire contents of the archive into the %path directory on your server.', array('!colorbox' => l($t('Colorbox plugin'), $library['download url']), '%path' => 'sites/all/libraries')),
);
}
elseif (version_compare($library['version'], COLORBOX_MIN_PLUGIN_VERSION, '>=')) {
$requirements['colorbox_plugin'] = array(
'title' => $t('Colorbox plugin'),
'severity' => REQUIREMENT_OK,
'value' => $colorbox_version,
'value' => $library['version'],
);
}
else {
......@@ -28,7 +35,7 @@ function colorbox_requirements($phase) {
'title' => $t('Colorbox plugin'),
'value' => $t('At least @a', array('@a' => COLORBOX_MIN_PLUGIN_VERSION)),
'severity' => REQUIREMENT_ERROR,
'description' => $t('You need to download the !colorbox and extract the entire contents of the archive into the %path folder of your server.', array('!colorbox' => l(t('Colorbox plugin'), 'http://colorpowered.com/colorbox/'), '%path' => $library_path)),
'description' => $t('You need to download a later version of the !colorbox and replace the old version located in the %path directory on your server.', array('!colorbox' => l($t('Colorbox plugin'), $library['download url']), '%path' => $library['library path'])),
);
}
}
......@@ -94,3 +101,18 @@ function colorbox_update_7201() {
return $ret;
}
/**
* Update the colorbox_compression_type variabel.
*/
function colorbox_update_7202() {
$ret = array();
if (variable_get('colorbox_compression_type', 'minified') == 'none') {
variable_set('colorbox_compression_type', 'source');
}
else {
variable_set('colorbox_compression_type', 'minified');
}
return $ret;
}
......@@ -71,6 +71,46 @@ function colorbox_views_api() {
);
}
/**
* Implements hook_libraries_info().
*/
function colorbox_libraries_info() {
$libraries['colorbox'] = array(
'name' => 'Colorbox plugin',
'vendor url' => 'http://www.jacklmoore.com/colorbox',
'download url' => 'http://www.jacklmoore.com/colorbox',
'path' => 'colorbox',
'version arguments' => array(
'file' => 'colorbox/jquery.colorbox-min.js',
'pattern' => '@ColorBox v([0-9\.a-z]+)@',
'lines' => 1,
),
'files' => array(
'js' => array(
'jquery.colorbox-min.js',
),
),
'variants' => array(
'minified' => array(
'files' => array(
'js' => array(
'jquery.colorbox-min.js',
),
),
),
'source' => array(
'files' => array(
'js' => array(
'jquery.colorbox.js',
),
),
),
),
);
return $libraries;
}
/**
* Implements hook_menu().
*/
......@@ -174,7 +214,8 @@ function _colorbox_doheader() {
drupal_add_js(array('colorbox' => $js_settings), array('type' => 'setting', 'scope' => JS_DEFAULT));
// Add and initialise the Colorbox plugin.
drupal_add_js(colorbox_get_js());
$variant = variable_get('colorbox_compression_type', 'minified');
libraries_load('colorbox', $variant);
drupal_add_js($path . '/js/colorbox.js');
// Add JS and CSS based on selected style.
......@@ -232,63 +273,6 @@ function colorbox_get_version($colorbox_js = NULL) {
return $version;
}
/**
* Return the JS filename for Colorbox plugin.
*
* @return
* Boolean indicating if the JS is located.
*/
function colorbox_get_js() {
$library_path = colorbox_get_path();
if (file_exists($library_path . '/colorbox/jquery.colorbox.js') && file_exists($library_path . '/colorbox/jquery.colorbox-min.js')) {
$colorbox_js_map = array('none' => 'jquery.colorbox.js', 'min' => 'jquery.colorbox-min.js');
$colorbox_js = $colorbox_js_map[variable_get('colorbox_compression_type', 'min')];
return $library_path . '/colorbox/' . $colorbox_js;
}
// else {
// drupal_set_message(t('You need to download the !colorbox and extract the entire contents of the archive into the %path folder of your server.', array('!colorbox' => l(t('Colorbox plugin'), 'http://colorpowered.com/colorbox/'), '%path' => $library_path)), 'error', FALSE);
// return FALSE;
// }
}
/**
* Return the path to the Colorbox plugin.
*/
function colorbox_get_path() {
static $library_path = NULL;
// Try to locate the library path in any possible setup.
if ($library_path == NULL) {
// First check the default location.
$path = variable_get('colorbox_path', COLORBOX_PATH);
if (is_dir($path . '/colorbox')) {
$library_path = $path;
}
// Ask the libraries module as a fallback.
elseif ($library_path == NULL && module_exists('libraries')) {
if ($path = libraries_get_path('colorbox')) {
$library_path = $path;
variable_set('colorbox_path', $library_path);
}
}
// HACK: If libraries api module is not installed but available, load it.
elseif ($library_path == NULL && file_exists(dirname(__FILE__) . '/../libraries/libraries.module')) {
require_once(dirname(__FILE__) . '/../libraries/libraries.module');
if ($path = libraries_get_path('colorbox')) {
$library_path = $path;
variable_set('colorbox_path', $library_path);
}
}
// If no path is found suggest the default one.
elseif ($library_path == NULL) {
$library_path = COLORBOX_PATH;
}
}
return $library_path;
}
/**
* Implements hook_field_formatter_info().
*/
......
......@@ -5,7 +5,6 @@
* Colorbox theme functions.
*/
/**
* Returns HTML for an Colorbox image field formatter.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment