Commit a0b64f7a authored by frjo's avatar frjo

More or less working on 8.0.0-beta2.

parent 27c32c9c
......@@ -5,26 +5,32 @@
* Colorbox theme functions.
*/
use Drupal\Core\Template\Attribute;
/**
* Returns HTML for an Colorbox image field formatter.
* Prepares variables for colorbox formatter templates.
*
* @param $variables
* An associative array containing:
* - item: An array of image data.
* - image_style: An optional image style.
* - path: An array containing the link 'path' and link 'options'.
* Default template: colorbox-formatter.html.twig.
*
* @ingroup themeable
* @param array $variables
* An associative array containing:
* - item: An ImageItem object.
* - item_attributes: An optional associative array of html attributes to be
* placed in the img tag.
* - entity: An entity object.
* - settings: Formatter settings array.
*/
function theme_colorbox_formatter($variables) {
function template_preprocess_colorbox_formatter(&$variables) {
$item = $variables['item'];
$item_attributes = isset($variables['item_attributes']) ? $variables['item_attributes'] : array();
$entity = $variables['entity'];
$settings = $variables['settings'];
$variables = array(); // Reset the $variables parameter.
// Build the caption.
$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':
......@@ -68,6 +74,10 @@ function theme_colorbox_formatter($variables) {
}
// Build the gallery id.
$id = $entity->id();
$entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
$field_name = $item->getParent()->getName();
switch ($settings['colorbox_gallery']) {
case 'post':
$gallery_id = 'gallery-' . $entity_id;
......@@ -89,42 +99,46 @@ function theme_colorbox_formatter($variables) {
$gallery_id = '';
}
$img_uri = $item->entity->getFileUri();
// Set ut the $variables['image'] parameter.
if (!empty($settings['colorbox_node_style'])) {
$variables['image'] = array(
'#theme' => 'image_style',
'#style_name' => $settings['colorbox_node_style'],
);
}
else {
$variables['image'] = array(
'#theme' => 'image',
);
}
$variables['image']['#attributes'] = $item_attributes;
// Do not output an empty 'title' attribute.
if (drupal_strlen($item->title) != 0) {
$variables['image']['#title'] = $item->title;
}
foreach (array('width', 'height', 'alt') as $key) {
$variables['image']["#$key"] = $item->$key;
}
$image_uri = $item->entity->getFileUri();
$variables['image']['#uri'] = empty($item->uri) ? $image_uri : $item->uri;
if (!empty($settings['colorbox_image_style'])) {
$style = entity_load('image_style', $settings['colorbox_image_style']);
$path = $style->buildUrl($img_uri);
$variables['url'] = $style->buildUrl($image_uri);
}
else {
$path = file_create_url($img_uri);
$variables['url'] = file_create_url($image_uri);
}
// 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);
$class = array('colorbox');
if ($settings['colorbox_node_style'] == 'hide') {
$class[] = 'js-hide';
}
$link_attributes = array(
'attributes' => array(
'title' => $caption,
'class' => $class,
'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 drupal_render($build);
$variables['attributes'] = new Attribute();
$variables['attributes']['title'] = $caption;
$variables['attributes']['rel'] = $gallery_id;
$variables['attributes']['class'] = $class;
}
......@@ -5,6 +5,8 @@
* Install, update and uninstall functions for the colorbox module.
*/
use Drupal\Core\Url;
/**
* Implements hook_requirements().
*/
......@@ -21,7 +23,7 @@ function colorbox_requirements($phase) {
'title' => t('Colorbox plugin'),
'value' => t('@e: At least @a', array('@e' => $error_type, '@a' => COLORBOX_MIN_PLUGIN_VERSION)),
'severity' => REQUIREMENT_ERROR,
'description' => t('!error You need to download the !colorbox, extract the archive and place the colorbox directory in the %path directory on your server.', array('!error' => $error_message, '!colorbox' => l(t('Colorbox plugin'), $library['download url']), '%path' => 'sites/all/libraries')),
'description' => t('!error You need to download the !colorbox, extract the archive and place the colorbox directory in the %path directory on your server.', array('!error' => $error_message, '!colorbox' => \Drupal::l(t('Colorbox plugin'), Url::fromUri($library['download url'])), '%path' => 'sites/all/libraries')),
);
}
elseif (version_compare($library['version'], COLORBOX_MIN_PLUGIN_VERSION, '>=')) {
......@@ -36,7 +38,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 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'])),
'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' => \Drupal::l(t('Colorbox plugin'), Url::fromUri($library['download url'])), '%path' => $library['library path'])),
);
}
}
......
colorbox.admin_settings:
title: 'Colorbox settings'
description: 'Configure the Colorbox settings.'
parent: system.admin_config_media
route_name: colorbox.admin_settings
colorbox.settings:
title: 'Colorbox'
parent: system.admin_config_media
route_name: colorbox.settings
description: 'Adjust Colorbox settings.'
weight: 20
......@@ -8,7 +8,7 @@
/**
* The default path to the Colorbox directory.
*/
define('COLORBOX_MIN_PLUGIN_VERSION', '1.3.21.1');
define('COLORBOX_MIN_PLUGIN_VERSION', '1.5.14');
/**
......@@ -22,11 +22,109 @@ function colorbox_theme() {
'entity' => NULL,
'settings' => NULL,
),
'file' => 'colorbox.theme.inc',
'file' => 'colorbox.inc',
),
);
}
/**
* Implements hook_page_build().
*/
function colorbox_page_build(&$page) {
$config = \Drupal::config('colorbox.settings');
if (!drupal_installation_attempted()) {
static $already_added = FALSE;
if ($already_added) {
return; // Don't add the JavaScript and CSS multiple times.
}
if (!_colorbox_active()) {
return; // Don't add the JavaScript and CSS on specified paths.
}
// Insert options and translated strings as javascript settings.
if ($config->get('custom.activate')) {
$js_settings = array(
'transition' => $config->get('custom.transition_type'),
'speed' => $config->get('custom.transition_speed'),
'opacity' => $config->get('custom.opacity'),
'slideshow' => $config->get('custom.slideshow.slideshow') ? TRUE : FALSE,
'slideshowAuto' => $config->get('custom.slideshow.auto') ? TRUE : FALSE,
'slideshowSpeed' => $config->get('custom.slideshow.speed'),
'slideshowStart' => $config->get('custom.slideshow.text_start'),
'slideshowStop' => $config->get('custom.slideshow.text_stop'),
'current' => $config->get('custom.text_current'),
'previous' => $config->get('custom.text_previous'),
'next' => $config->get('custom.text_next'),
'close' => $config->get('custom.text_close'),
'overlayClose' => $config->get('custom.overlayclose') ? TRUE : FALSE,
'maxWidth' => $config->get('custom.maxwidth'),
'maxHeight' => $config->get('custom.maxheight'),
'initialWidth' => $config->get('custom.initialwidth'),
'initialHeight' => $config->get('custom.initialheight'),
'fixed' => $config->get('custom.fixed') ? TRUE : FALSE,
'scrolling' => $config->get('custom.scrolling') ? TRUE : FALSE,
'mobiledetect' => $config->get('advanced.mobile_detect') ? TRUE : FALSE,
'mobiledevicewidth' => $config->get('advanced.mobile_device_width'),
);
}
else {
$js_settings = array(
'opacity' => '0.85',
'current' => t('{current} of {total}'),
'previous' => t('« Prev'),
'next' => t('Next »'),
'close' => t('Close'),
'maxWidth' => '98%',
'maxHeight' => '98%',
'fixed' => TRUE,
'mobiledetect' => $config->get('advanced.mobile_detect') ? TRUE : FALSE,
'mobiledevicewidth' => $config->get('advanced.mobile_device_width'),
);
}
$path = drupal_get_path('module', 'colorbox');
$style = $config->get('custom.style');
// Give other modules the possibility to override Colorbox settings and style.
\Drupal::moduleHandler()->alter('colorbox_settings', $js_settings, $style);
$page['#attached']['js'][] = array(
'data' => array('colorbox' => $js_settings),
'type' => 'setting',
);
// Add and initialise the Colorbox plugin.
$variant = $config->get('advanced.compression_type');
libraries_load('colorbox', $variant);
$page['#attached']['js'][$path . '/js/colorbox.js'] = array();
// Add JS and CSS based on selected style.
switch ($style) {
case 'none':
break;
case 'default':
case 'plain':
case 'stockholmsyndrome':
$page['#attached']['css'][$path . '/styles/' . $style . '/colorbox_style.css'] = array();
$page['#attached']['js'][$path . '/styles/' . $style . '/colorbox_style.js'] = array();
break;
default:
$page['#attached']['css'][$path . '/styles/' . $style . '/colorbox.css'] = array();
}
if ($config->get('extra.load', 0)) {
//$page['#attached']['js'][$path . '/js/colorbox_load.js'] = array();
}
if ($config->get('extra.inline', 0)) {
//$page['#attached']['js'][$path . '/js/colorbox_inline.js'] = array();
}
$already_added = TRUE;
}
}
/**
* Implements hook_libraries_info().
*/
......@@ -37,7 +135,7 @@ function colorbox_libraries_info() {
'download url' => 'https://github.com/jackmoore/colorbox/archive/1.x.zip',
'version arguments' => array(
'file' => 'jquery.colorbox-min.js',
'pattern' => '@(?i:Colorbox) v([0-9\.a-z]+)@',
'pattern' => '@(?i:Colorbox)\sv?([0-9\.a-z]+)@',
'lines' => 5,
),
'files' => array(
......@@ -89,7 +187,7 @@ function _colorbox_active() {
// 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));
$path_alias = drupal_strtolower(\Drupal::service('path.alias_storage')->lookupPathAlias($path, 'en'));
$page_match = drupal_match_path($path_alias, $pages);
if ($path_alias != $path) {
$page_match = $page_match || drupal_match_path($path, $pages);
......
colorbox.settings:
colorbox.admin_settings:
path: '/admin/config/media/colorbox'
defaults:
_title: 'Colorbox'
_form: 'Drupal\colorbox\Form\ColorboxSettingsForm'
_form: '\Drupal\colorbox\Form\ColorboxSettingsForm'
_title: 'Colorbox settings'
requirements:
_permission: 'administer site configuration'
services:
colorbox.subscriber:
class: Drupal\colorbox\EventSubscriber\ColorboxSubscriber
arguments: ['@module_handler']
tags:
- { name: event_subscriber }
<?php
/**
* @file
* Definition of Drupal\colorbox\EventSubscriber\ColorboxSubscriber.
*/
namespace Drupal\colorbox\EventSubscriber;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
* KernelEvents::REQUEST subscriber for colorbox loading.
*/
class ColorboxSubscriber implements EventSubscriberInterface {
/**
* The The module handler used to hook altering.
*
* @var Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $module_handler;
/**
* Construct the ColorboxSubscriber.
*
* @param Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler used to hook altering.
*/
public function __construct(ModuleHandlerInterface $module_handler) {
$this->module_handler = $module_handler;
}
/**
* Loads Colorbox library.
*
* @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The Event to process.
*/
public function colorboxLoadLibrary(GetResponseEvent $event) {
$config = \Drupal::config('colorbox.settings');
if (!drupal_installation_attempted()) {
static $already_added = FALSE;
if ($already_added) {
return; // Don't add the JavaScript and CSS multiple times.
}
if (!_colorbox_active()) {
return; // Don't add the JavaScript and CSS on specified paths.
}
// Insert options and translated strings as javascript settings.
if ($config->get('custom.activate')) {
$js_settings = array(
'transition' => $config->get('custom.transition_type'),
'speed' => $config->get('custom.transition_speed'),
'opacity' => $config->get('custom.opacity'),
'slideshow' => $config->get('custom.slideshow.slideshow') ? TRUE : FALSE,
'slideshowAuto' => $config->get('custom.slideshow.auto') ? TRUE : FALSE,
'slideshowSpeed' => $config->get('custom.slideshow.speed'),
'slideshowStart' => $config->get('custom.slideshow.text_start'),
'slideshowStop' => $config->get('custom.slideshow.text_stop'),
'current' => $config->get('custom.text_current'),
'previous' => $config->get('custom.text_previous'),
'next' => $config->get('custom.text_next'),
'close' => $config->get('custom.text_close'),
'overlayClose' => $config->get('custom.overlayclose') ? TRUE : FALSE,
'maxWidth' => $config->get('custom.maxwidth'),
'maxHeight' => $config->get('custom.maxheight'),
'initialWidth' => $config->get('custom.initialwidth'),
'initialHeight' => $config->get('custom.initialheight'),
'fixed' => $config->get('custom.fixed') ? TRUE : FALSE,
'scrolling' => $config->get('custom.scrolling') ? TRUE : FALSE,
'mobiledetect' => $config->get('advanced.mobile_detect') ? TRUE : FALSE,
'mobiledevicewidth' => $config->get('advanced.mobile_device_width'),
);
}
else {
$js_settings = array(
'opacity' => '0.85',
'current' => t('{current} of {total}'),
'previous' => t('« Prev'),
'next' => t('Next »'),
'close' => t('Close'),
'maxWidth' => '98%',
'maxHeight' => '98%',
'fixed' => TRUE,
'mobiledetect' => $config->get('advanced.mobile_detect') ? TRUE : FALSE,
'mobiledevicewidth' => $config->get('advanced.mobile_device_width'),
);
}
$path = drupal_get_path('module', 'colorbox');
$style = $config->get('custom.style');
// Give other modules the possibility to override Colorbox settings and style.
$data = &$js_settings;
$this->module_handler->alter('colorbox_settings', $data, $style);
$settings = array(
'#attached' => array(
'js' => array(
array(
'data' => array('colorbox' => $js_settings),
'type' => 'setting',
),
),
),
);
drupal_render($settings);
// Add and initialise the Colorbox plugin.
$variant = $config->get('advanced.compression_type');
libraries_load('colorbox', $variant);
$colorbox = array(
'#attached' => array(
'js' => array(
$path . '/js/colorbox.js' => array(),
),
),
);
drupal_render($colorbox);
// Add JS and CSS based on selected style.
switch ($style) {
case 'none':
break;
case 'default':
case 'plain':
case 'stockholmsyndrome':
$style_render = array(
'#attached' => array(
'css' => array(
$path . '/styles/' . $style . '/colorbox_style.css' => array(),
),
'js' => array(
$path . '/styles/' . $style . '/colorbox_style.js' => array(),
),
),
);
drupal_render($style_render);
break;
default:
$style_render = array(
'#attached' => array(
'css' => array(
$path . '/styles/' . $style . '/colorbox.css' => array(),
),
),
);
drupal_render($style_render);
}
if ($config->get('extra.load', 0)) {
$load = array(
'#attached' => array(
'js' => array(
$path . '/js/colorbox_load.js' => array(),
),
),
);
drupal_render($load);
}
if ($config->get('extra.inline', 0)) {
$inline = array(
'#attached' => array(
'js' => array(
$path . '/js/colorbox_inline.js' => array(),
),
),
);
drupal_render($inline);
}
$already_added = TRUE;
}
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('colorboxLoadLibrary', 50);
return $events;
}
}
......@@ -9,6 +9,7 @@
namespace Drupal\colorbox\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* General configuration form for controlling the colorbox behaviour..
......@@ -19,15 +20,15 @@ class ColorboxSettingsForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function getFormID() {
return 'colorbox_admin_settings';
return 'colorbox_admin_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
public function buildForm(array $form, FormStateInterface $form_state) {
// Get all settings
$config = $this->configFactory->get('colorbox.settings');
$config = $this->config('colorbox.settings');
$form['#attached']['js'][] = array('data' => drupal_get_path('module', 'colorbox') . '/js/colorbox_admin_settings.js', 'preprocess' => FALSE);
......@@ -98,7 +99,7 @@ class ColorboxSettingsForm extends ConfigFormBase {
$form['colorbox_custom_settings']['colorbox_transition_speed'] = array(
'#type' => 'select',
'#title' => t('Transition speed'),
'#options' => array_map('format_interval', array_combine($speed_options, $speed_options)),
'#options' => 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.'),
);
......@@ -106,7 +107,7 @@ class ColorboxSettingsForm extends ConfigFormBase {
$form['colorbox_custom_settings']['colorbox_opacity'] = array(
'#type' => 'select',
'#title' => t('Opacity'),
'#options' => array_map('format_interval', array_combine($opacity_options, $opacity_options)),
'#options' => array_combine($opacity_options, $opacity_options),
'#default_value' => $config->get('custom.opacity'),
'#description' => t('The overlay opacity level. Range: 0 to 1.'),
);
......@@ -316,7 +317,7 @@ class ColorboxSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
// Get config factory
$config = $this->configFactory->get('colorbox.settings');
......
......@@ -9,6 +9,7 @@ namespace Drupal\colorbox\Plugin\Field\FieldFormatter;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'colorbox' formatter.
......@@ -42,7 +43,7 @@ class ColorboxFormatter extends ImageFormatterBase {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state) {
public function settingsForm(array $form, FormStateInterface $form_state) {
$image_styles = image_style_options(FALSE);
$image_styles_hide = $image_styles;
$image_styles_hide['hide'] = t('Hide (do not display image)');
......@@ -201,17 +202,34 @@ class ColorboxFormatter extends ImageFormatterBase {
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
$index = $this->getSetting('colorbox_multivalue_index');
$entity = $items->getEntity();
$settings = $this->getSettings();
// Collect cache tags to be added for each item in the field.
$cache_tags = array();
if (!empty($settings['colorbox_node_style'])) {
$image_style = entity_load('image_style', $settings['colorbox_node_style']);
$cache_tags = $image_style->getCacheTag();
}
foreach ($items as $delta => $item) {
if ($index === NULL || $index === '' || $index === $delta) {
if ($item->entity && ($index === NULL || $index === '' || $index === $delta)) {
// 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);
$elements[$delta] = array(
'#theme' => 'colorbox_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#entity' => $entity,
'#settings' => $settings,
'#cache' => array(
'tags' => $cache_tags,
),
);
}
}
......
{#
/**
* @file
* Default theme implementation to display a formatted colorbox image field.
*
* Available variables:
* - image: A collection of image data.
* - url: An URL the image can be linked to.
* - attributes: Link attributes.
*
* @see template_preprocess_colorbox_formatter()
*
* @ingroup themeable
*/
#}
<a href="{{ url }}"{{ attributes }}>{{ image }}</a>
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