Commit 02e6a777 authored by frjo's avatar frjo

Some efforts to make Colorbox work with Drupal 8 alpha11.

parent f3bc65ec
name: 'Colorbox'
type: module
description: 'A light-weight, customizable lightbox plugin for jQuery 1.4.3+.'
core: 8.x
configure: admin/config/media/colorbox
package: Media
core: 8.x
configure: colorbox.settings
dependencies:
- libraries
......@@ -43,123 +43,3 @@ function colorbox_requirements($phase) {
return $requirements;
}
/**
* Delete the unused colorbox_login_form variable.
*/
function colorbox_update_7001() {
$ret = array();
variable_del('colorbox_login_form');
return $ret;
}
/**
* Delete the unused colorbox_title_trim and
* colorbox_title_trim_length variables.
*/
function colorbox_update_7002() {
$ret = array();
$colorbox_title_trim = variable_get('colorbox_title_trim', NULL);
$colorbox_title_trim_length = variable_get('colorbox_title_trim_length', NULL);
if (!empty($colorbox_title_trim)) {
variable_set('colorbox_caption_trim', $colorbox_title_trim);
}
if (!empty($colorbox_title_trim_length)) {
variable_set('colorbox_caption_trim_length', $colorbox_title_trim_length);
}
variable_del('colorbox_title_trim');
variable_del('colorbox_title_trim_length');
return $ret;
}
/**
* Delete the unused colorbox_login and colorbox_login_links variables.
*/
function colorbox_update_7200() {
$ret = array();
variable_del('colorbox_login');
variable_del('colorbox_login_links');
return $ret;
}
/**
* Delete the unused colorbox_auto_image_nodes variable.
*/
function colorbox_update_7201() {
$ret = array();
variable_del('colorbox_auto_image_nodes');
return $ret;
}
/**
* Update the colorbox_compression_type variable.
*/
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;
}
/**
* Converts variables to configuration.
*/
function colorbox_update_8000() {
update_variables_to_config(
'colorbox.settings',
array(
'colorbox_load' => 'extra.load',
'colorbox_inline' => 'extra.inline',
'colorbox_style' => 'custom.style',
'colorbox_custom_settings_activate' => 'custom.activate',
'colorbox_transition_type' => 'custom.transition_type',
'colorbox_transition_speed' => 'custom.transition_speed',
'colorbox_opacity' => 'custom.opacity',
'colorbox_text_current' => 'custom.text_current',
'colorbox_text_previous' => 'custom.text_previous',
'colorbox_text_next' => 'custom.text_next',
'colorbox_text_close' => 'custom.text_close',
'colorbox_overlayclose' => 'custom.overlayclose',
'colorbox_maxwidth' => 'custom.maxwidth',
'colorbox_maxheight' => 'custom.maxheight',
'colorbox_initialwidth' => 'custom.initialwidth',
'colorbox_initialheight' => 'custom.initialheight',
'colorbox_fixed' => 'custom.fixed',
'colorbox_scrolling' => 'custom.scrolling',
'colorbox_slideshow' => 'custom.slideshow.slideshow',
'colorbox_slideshowauto' => 'custom.slideshow.auto',
'colorbox_slideshowspeed' => 'custom.slideshow.speed',
'colorbox_text_start' => 'custom.slideshow.text_start',
'colorbox_text_stop' => 'custom.slideshow.text_stop',
'colorbox_mobile_detect' => 'advanced.mobile_detect',
'colorbox_mobile_device_width' => 'advanced.mobile_device_width',
'colorbox_caption_trim' => 'advanced.caption_trim',
'colorbox_caption_trim_length' => 'advanced.caption_trim_length',
'colorbox_visibility' => 'advanced.visibility',
'colorbox_compression_type' => 'advanced.compression_type',
'colorbox_image_style' => 'insert.image_style',
'colorbox_insert_gallery' => 'insert.insert_gallery',
)
);
$sql = db_select('variable', 'v')
->fields('v', array('value'))
->condition('name', 'colorbox_pages')
->execute();
if ($row = $sql->fetchAssoc()) {
$val = unserialize($row['value']);
$pages = _colorbox_string_textarea_to_array($val);
$conf = config('colorbox.settings');
$conf->set('advanced.pages', $pages);
}
}
core = 7.x
api = 2
libraries[colorbox][type] = "libraries"
libraries[colorbox][download][type] = "file"
libraries[colorbox][download][url] = "https://github.com/jackmoore/colorbox/archive/1.x.zip"
libraries[colorbox][directory_name] = "colorbox"
......
colorbox.settings:
title: 'Colorbox'
parent: system.admin_config_media
route_name: colorbox.settings
description: 'Adjust Colorbox settings.'
weight: 20
......@@ -16,28 +16,9 @@ define('COLORBOX_MIN_PLUGIN_VERSION', '1.3.21.1');
*/
function colorbox_theme() {
return array(
'colorbox_imagefield' => array(
'variables' => array(
'image' => array(),
'path' => NULL,
'title' => NULL,
'gid' => NULL,
),
'file' => 'colorbox.theme.inc',
),
'colorbox_insert_image' => array(
'colorbox_formatter' => array(
'variables' => array(
'item' => NULL,
'widget' => NULL,
),
'template' => 'colorbox-insert-image',
'file' => 'colorbox.theme.inc',
),
'colorbox_image_formatter' => array(
'variables' => array(
'image' => NULL,
'entity' => NULL,
'settings' => NULL,
),
......@@ -85,21 +66,6 @@ function colorbox_libraries_info() {
return $libraries;
}
/**
* Implements hook_menu().
*/
function colorbox_menu() {
$items = array();
$items['colorbox.settings'] = array(
'title' => 'Colorbox',
'description' => 'Adjust Colorbox settings.',
'route_name' => 'colorbox.settings',
);
return $items;
}
/**
* Check if Colorbox should be active for the current URL.
*
......@@ -113,60 +79,33 @@ function _colorbox_active() {
return FALSE;
}
$config = \Drupal::config('colorbox.settings');
$current_path = current_path();
// Code from the block_list funtion in block.module.
// Assume there are no matches until one is found.
$page_match = FALSE;
$path = Drupal::service('path.alias_manager.cached')->getPathAlias($current_path);
$colorbox_pages = _colorbox_array_to_string_textarea($config->get('advanced.pages'));
// Compare with the internal and path alias (if any).
$page_match = drupal_match_path($path, $colorbox_pages);
if ($path != $current_path) {
$page_match = $page_match || drupal_match_path($current_path, $colorbox_pages);
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$config = \Drupal::config('colorbox.settings');
$pages = drupal_strtolower(_colorbox_array_to_string($config->get('advanced.pages')));
// Compare the lowercase path alias (if any) and internal path.
$path = current_path();
$path_alias = drupal_strtolower(\Drupal::service('path.alias_manager.cached')->getPathAlias($path));
$page_match = drupal_match_path($path_alias, $pages);
if ($path_alias != $path) {
$page_match = $page_match || drupal_match_path($path, $pages);
}
$page_match = $config->get('advanced.visibility') == 0 ? !$page_match : $page_match;
return $page_match;
}
/**
* Implements hook_insert_styles().
*/
function colorbox_insert_styles() {
$insert_styles = array();
foreach (image_styles() as $key => $style) {
$insert_styles['colorbox__' . $key] = array('label' => t('Colorbox @style', array('@style' => $style['name'])));
}
return $insert_styles;
}
/**
* Implements hook_insert_content().
*/
function colorbox_insert_content($item, $style, $widget) {
list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
return theme('colorbox_insert_image', array('item' => $item, 'widget' => $widget));
}
/**
* Machine names normally need to be unique but that does not apply to galleries.
*
* @return
* Always FALSE
*/
function colorbox_gallery_exists() {
return FALSE;
}
/**
* Converts a text with lines (\n) into an array of lines.
*
* @return
* Array with as many items as non-empty lines in the text
*/
function _colorbox_string_textarea_to_array($text) {
function _colorbox_string_to_array($text) {
$text = str_replace("\r\n", "\n", $text);
return array_filter(explode("\n", $text), 'trim');
}
......@@ -177,6 +116,6 @@ function _colorbox_string_textarea_to_array($text) {
* @return
* Text with lines
*/
function _colorbox_array_to_string_textarea($array) {
function _colorbox_array_to_string($array) {
return implode("\r\n", $array);
}
......@@ -2,6 +2,6 @@ colorbox.settings:
path: '/admin/config/media/colorbox'
defaults:
_title: 'Colorbox'
_form: '\Drupal\colorbox\Form\ColorboxSettingsForm'
_form: 'Drupal\colorbox\Form\ColorboxSettingsForm'
requirements:
_permission: 'administer site configuration'
......@@ -16,33 +16,26 @@
*
* @ingroup themeable
*/
function theme_colorbox_image_formatter($variables) {
$image = array(
'uri' => $variables['image']->entity->getFileUri(),
'alt' => $variables['image']->alt,
'title' => $variables['image']->title,
'style_name' => $variables['settings']['colorbox_node_style'],
);
//FFFif (isset($item['width']) && isset($item['height'])) {
//FFF$image['width'] = $item['width'];
//FFF$image['height'] = $item['height'];
//FFF}
$entity_title = $variables['image']->getEntity()->label();
$entity_type = $variables['entity']->entityType();
switch ($variables['settings']['colorbox_caption']) {
function theme_colorbox_formatter($variables) {
$item = $variables['item'];
$entity = $variables['entity'];
$settings = $variables['settings'];
$entity_title = $entity->label();
$id = $entity->id();
$entity_type = $entity->bundle();
$entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
$field_name = $item->getParent()->getName();
switch ($settings['colorbox_caption']) {
case 'auto':
// If the title is empty use alt or the entity title in that order.
if ($variables['image']->title) {
$caption = $variables['image']->title;
if (!empty($item->title)) {
$caption = $item->title;
}
elseif ($variables['image']->alt) {
$caption = $variables['image']->alt;
elseif (!empty($item->alt)) {
$caption = $item->alt;
}
elseif ($entity_title) {
elseif (!empty($entity_title)) {
$caption = $entity_title;
}
else {
......@@ -50,41 +43,32 @@ function theme_colorbox_image_formatter($variables) {
}
break;
case 'title':
$caption = $variables['image']->title;
$caption = $item->title;
break;
case 'alt':
$caption = $variables['image']->alt;
$caption = $item->alt;
break;
case 'node_title':
case 'entity_title':
$caption = $entity_title;
break;
case 'custom':
$token_service = \Drupal::token();
$caption = $token_service->replace(
$variables['settings']['colorbox_caption_custom'],
array(
$entity_type => $variables['entity'],
'file' => $variables['image']),
array('clear' => TRUE)
);
$caption = $token_service->replace($settings['colorbox_caption_custom'], array($entity_type => $entity, 'file' => $item), array('clear' => TRUE));
break;
default:
$caption = '';
}
// Shorten the caption for the example styles or when caption shortening is active.
$colorbox_style = variable_get('colorbox_style', 'default');
$trim_length = variable_get('colorbox_caption_trim_length', 75);
if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || variable_get('colorbox_caption_trim', 0)) && (drupal_strlen($caption) > $trim_length)) {
$config = \Drupal::config('colorbox.settings');
$colorbox_style = $config->get('colorbox_style');
$trim_length = $config->get('colorbox_caption_trim_length');
if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || $config->get('colorbox_caption_trim')) && (drupal_strlen($caption) > $trim_length)) {
$caption = drupal_substr($caption, 0, $trim_length - 5) . '...';
}
// Build the gallery id.
$id = $variables['entity']->id();
$entity_bundle = $variables['entity']->getType();
$entity_id = !empty($id) ? $entity_bundle . '-' . $id : 'entity-id';
$field = $variables['image']->getParent()->getName();
switch ($variables['settings']['colorbox_gallery']) {
switch ($settings['colorbox_gallery']) {
case 'post':
$gallery_id = 'gallery-' . $entity_id;
break;
......@@ -92,110 +76,56 @@ function theme_colorbox_image_formatter($variables) {
$gallery_id = 'gallery-all';
break;
case 'field_post':
$gallery_id = 'gallery-' . $entity_id . '-' . $field;
$gallery_id = 'gallery-' . $entity_id . '-' . $field_name;
break;
case 'field_page':
$gallery_id = 'gallery-' . $field;
$gallery_id = 'gallery-' . $field_name;
break;
case 'custom':
$token_service = \Drupal::token();
$gallery_id = $token_service->replace(
$variables['settings']['colorbox_gallery_custom'],
array(
$entity_type => $variables['entity'],
'file' => $variables['image']),
array('clear' => TRUE)
);
$gallery_id = $token_service->replace($settings['colorbox_gallery_custom'], array($entity_type => $entity, 'file' => $item), array('clear' => TRUE));
break;
default:
$gallery_id = '';
}
if ($style_name = $variables['settings']['colorbox_image_style']) {
$style = entity_load('image_style', $style_name);
$path = $style->buildUrl($image['uri']);
$img_uri = $item->entity->getFileUri();
if (!empty$settings['colorbox_image_style'])) {
$style = entity_load('image_style', $settings['colorbox_image_style']);
$path = $style->buildUrl($img_uri);
}
else {
$path = file_create_url($image['uri']);
$path = file_create_url($img_uri);
}
return theme('colorbox_imagefield', array('image' => $image, 'path' => $path, 'title' => $caption, 'gid' => $gallery_id));
}
// Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them.
$item_attributes = $item->_attributes;
unset($item->_attributes);
/**
* Returns HTML for an image using a specific Colorbox image style.
*
* @param $variables
* An associative array containing:
* - image: image item as array.
* - path: The path of the image that should be displayed in the Colorbox.
* - title: The title text that will be used as a caption in the Colorbox.
* - gid: Gallery id for Colorbox image grouping.
*
* @ingroup themeable
*/
function theme_colorbox_imagefield($variables) {
$class = array('colorbox');
if ($variables['image']['style_name'] == 'hide') {
$image = '';
if ($settings['colorbox_node_style'] == 'hide') {
$item = NULL;
$class[] = 'js-hide';
}
elseif (!empty($variables['image']['style_name'])) {
$image = theme('image_style', $variables['image']);
}
else {
$image = theme('image', $variables['image']);
}
$options = array(
'html' => TRUE,
$link_attributes = array(
'attributes' => array(
'title' => $variables['title'],
'title' => $caption,
'class' => $class,
'rel' => $variables['gid'],
)
'rel' => $gallery_id,
),
);
$link = array('path' => $path, 'options' => $link_attributes);
$build = array(
'#theme' => 'image_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $settings['colorbox_node_style'],
'#path' => $link,
);
return l($image, $variables['path'], $options);
}
/**
* Preprocess variables for the colorbox-insert-image.html.twig file.
*/
function template_preprocess_colorbox_insert_image(&$variables) {
$class = array();
$file = file_load($variables['item']['fid']);
if (!empty($variables['widget']['settings']['insert_class'])) {
$class = explode(' ', $variables['widget']['settings']['insert_class']);
}
$class[] = 'image-' . $variables['item']['style_name'];
foreach ($class as $key => $value) {
$class[$key] = drupal_html_class($value);
}
$variables['image_path'] = image_style_url($variables['item']['style_name'], $file->uri);
if ($style_name = variable_get('colorbox_image_style', '')) {
$variables['link_path'] = image_style_url($style_name, $file->uri);
}
else {
$variables['link_path'] = file_create_url($file->uri);
}
$variables['class'] = implode(' ', $class);
$variables['gallery_id'] = '';
switch (variable_get('colorbox_insert_gallery', 0)) {
case 0:
case 1:
case 2:
$variables['gallery_id'] = 'gallery-all';
break;
case 3:
$variables['gallery_id'] = '';
break;
}
return drupal_render($build);
}
......@@ -108,13 +108,3 @@ colorbox.settings:
compression_type:
type: string
label: 'Colorbox compression level'
insert:
type: mapping
label: 'Insert module settings'
mapping:
image_style:
type: string
label: 'Image style'
insert_gallery:
type: integer
label: 'Insert image gallery'
......@@ -58,20 +58,10 @@ function colorbox_drush_command() {
function colorbox_drush_help($section) {
switch ($section) {
case 'drush:colorbox-plugin':
return dt('Download and install the Colorbox plugin from jacklmoore.com/colorbox, default location is sites/all/libraries.');
return dt('Download and install the Colorbox plugin from jacklmoore.com/colorbox, default location is the libraries directory.');
}
}
/**
* Implements drush_MODULE_post_pm_enable().
*/
// function drush_colorbox_post_pm_enable() {
// $modules = func_get_args();
// if (in_array('colorbox', $modules)) {
// drush_colorbox_plugin();
// }
// }
/**
* Command to download the Colorbox plugin.
*/
......@@ -81,7 +71,7 @@ function drush_colorbox_plugin() {
$path = $args[0];
}
else {
$path = 'sites/all/libraries';
$path = 'libraries';
}
// Create the path if it does not exist.
......
(function ($) {
(function ($, Drupal) {
Drupal.behaviors.initColorbox = {
attach: function (context, settings) {
if (!$.isFunction($.colorbox)) {
return;
}
"use strict";
if (settings.colorbox.mobiledetect && window.matchMedia) {
// Disable Colorbox for small screens.
mq = window.matchMedia("(max-device-width: " + settings.colorbox.mobiledevicewidth + ")");
if (mq.matches) {
Drupal.behaviors.initColorbox = {
attach: function (context, settings) {
if (!$.isFunction($.colorbox)) {
return;
}
}
$('.colorbox', context)
.once('init-colorbox')
.colorbox(settings.colorbox);
}
};
if (settings.colorbox.mobiledetect && window.matchMedia) {
// Disable Colorbox for small screens.
var mq = window.matchMedia("(max-device-width: " + settings.colorbox.mobiledevicewidth + ")");
if (mq.matches) {
return;
}
}
{
$(document).bind('cbox_complete', function () {
Drupal.attachBehaviors('#cboxLoadedContent');
});
}
$('.colorbox', context)
.once('init-colorbox')
.colorbox(settings.colorbox);
$(context).bind('cbox_complete', function () {
Drupal.attachBehaviors('#cboxLoadedContent');
});
}
};
})(jQuery);
})(jQuery, Drupal);
<?php
/**
* @file
* Administrative class form for the colorbox module.
......@@ -8,8 +9,6 @@
namespace Drupal\colorbox\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Config\ConfigFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* General configuration form for controlling the colorbox behaviour..
......@@ -27,44 +26,13 @@ class ColorboxSettingsForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
// Get all settings
$config = $this->configFactory->get('colorbox.settings');
// Get all settings
$config = $this->configFactory->get('colorbox.settings');
$conf_js = array(
'#attached' => array(
'js' => array(
drupal_get_path('module', 'colorbox') . '/js/colorbox_admin_settings.js' => array('preprocess' => FALSE),
),
),
);
drupal_render($conf_js);
$form['#attached']['js'][] = array('data' => drupal_get_path('module', 'colorbox') . '/js/colorbox_admin_settings.js', 'preprocess' => FALSE);
$library = libraries_detect('colorbox');
if (module_exists('insert')) {
$form['colorbox_insert_module'] = array(
'#type' => 'fieldset',
'#title' => t('Insert module settings'),
);
$image_styles = image_style_options(FALSE);
$form['colorbox_insert_module']['colorbox_image_style'] = array(
'#type' => 'select',
'#title' => t('Image style'),
'#empty_option' => t('None (original image)'),
'#options' => $image_styles,
'#default_value' => $config->get('insert.image_style'),
'#description' => t('Select which image style to use for viewing images in the colorbox.'),
);
$form['colorbox_insert_module']['colorbox_insert_gallery'] = array(
'#type' => 'radios',
'#title' => t('Insert image gallery'),
'#default_value' => $config->get('insert.insert_gallery'),
'#options' => array(0 => t('Per page gallery'), 3 => t('No gallery')),
'#description' => t('Should the gallery be all images on the page (default) or disabled.'),
);
}
$form['colorbox_extra_features'] = array(
'#type' => 'fieldset',
'#title' => t('Extra features'),
......@@ -126,17 +94,19 @@ class ColorboxSettingsForm extends ConfigFormBase {
'#default_value' => $config->get('custom.transition_type'),
'#description' => t('The transition type.'),
);
$speed_options = array(100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600);
$form['colorbox_custom_settings']['colorbox_transition_speed'] = array(
'#type' => 'select',
'#title' => t('Transition speed'),
'#options' => drupal_map_assoc(array(100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600)),
'#options' => array_map('format_interval', array_combine($speed_options, $speed_options)),
'#default_value' => $config->get('custom.transition_speed'),
'#description' => t('Sets the speed of the fade and elastic transitions, in milliseconds.'),
);
$opacity_options = array('0', '0.10', '0.15', '0.20', '0.25', '0.30', '0.35', '0.40', '0.45', '0.50', '0.55', '0.60', '0.65', '0.70', '0.75', '0.80', '0.85', '0.90', '0.95', '1');
$form['colorbox_custom_settings']['colorbox_opacity'] = array(
'#type' => 'select',
'#title' => t('Opacity'),
'#options' => drupal_map_assoc(array('0', '0.10', '0.15', '0.20', '0.25', '0.30', '0.35', '0.40', '0.45', '0.50', '0.55', '0.60', '0.65', '0.70', '0.75', '0.80', '0.85', '0.90', '0.95', '1')),
'#options' => array_map('format_interval', array_combine($opacity_options, $opacity_options)),
'#default_value' => $config->get('custom.opacity'),
'#description' => t('The overlay opacity level. Range: 0 to 1.'),
);
......@@ -243,10 +213,11 @@ class ColorboxSettingsForm extends ConfigFormBase {
'#default_value' => $config->get('custom.slideshow.auto'),
'#description' => t('If the slideshow should automatically start to play.'),
);
$slideshow_options = array(1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000);
$form['colorbox_custom_settings']['colorbox_slideshow_settings']['colorbox_slideshowspeed'] = array(
'#type' => 'select',
'#title' => t('Slideshow speed'),
'#options' => drupal_map_assoc(array(1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000)),
'#options' => array_map('format_interval', array_combine($slideshow_options, $slideshow_options)),
'#default_value' => $config->get('custom.slideshow.speed'),
'#description' => t('Sets the speed of the slideshow, in milliseconds.'),
);
......@@ -305,10 +276,11 @@ class ColorboxSettingsForm extends ConfigFormBase {
'#default_value' => $config->get('advanced.caption_trim'),
'#description' => t('If the caption should be made shorter in the Colorbox to avoid layout problems. The default is to shorten for the example styles, they need it, but not for other styles.'),
);