Commit c84536e6 authored by Sam152's avatar Sam152 Committed by frjo
Browse files

Issue #2642156 by Sam152: Move the logic contained within...

Issue #2642156 by Sam152: Move the logic contained within hook_page_attachments to an AttachmentManager service
parent f5309364
......@@ -31,81 +31,10 @@ function colorbox_theme() {
* Implements hook_page_attachments().
*/
function colorbox_page_attachments(array &$page) {
if (drupal_installation_attempted()) {
return; // Don't add the JavaScript and CSS during installation.
$attachment = \Drupal::service('colorbox.attachment');
if ($attachment->isApplicable()) {
$attachment->attach($page);
}
static $already_added = FALSE;
if ($already_added) {
return; // Don't add the JavaScript and CSS multiple times.
}
if (!\Drupal::service('colorbox.activation_check')->isActive()) {
return; // Don't add the JavaScript and CSS on specified paths.
}
// Insert options and translated strings as javascript settings.
$config = \Drupal::config('colorbox.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'),
);
}
$style = $config->get('custom.style');
// Give other modules the possibility to override Colorbox settings and style.
\Drupal::moduleHandler()->alter('colorbox_settings', $js_settings, $style);
// Add colorbox js settings.
$page['#attached']['drupalSettings']['colorbox'] = $js_settings;
// Add and initialise the Colorbox plugin.
if ($config->get('advanced.compression_type' == 'minified')) {
$page['#attached']['library'][] = 'colorbox/colorbox';
}
else {
$page['#attached']['library'][] = 'colorbox/colorbox-dev';
}
// Add JS and CSS based on selected style.
if ($style != 'none') {
$page['#attached']['library'][] = "colorbox/$style";
}
$already_added = TRUE;
}
/**
......
......@@ -2,3 +2,6 @@ services:
colorbox.activation_check:
class: Drupal\colorbox\ActivationCheck
arguments: ['@config.factory', '@path.matcher', '@path.current', '@path.alias_storage']
colorbox.attachment:
class: Drupal\colorbox\ColorboxAttachment
arguments: ['@colorbox.activation_check', '@module_handler', '@config.factory']
<?php
/**
* @file
* Contains Drupal\colorbox\PageAttachmentInterface.
*/
namespace Drupal\colorbox;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
/**
* An implementation of PageAttachmentInterface for the colorbox library.
*/
class ColorboxAttachment implements PageAttachmentInterface {
/**
* The service to determin if colorbox should be activated.
*
* @var \Drupal\colorbox\ActivationCheckInterface
*/
protected $activation;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The colorbox settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $settings;
/**
* If colorbox has already been added to the page.
*
* @var bool
*/
static $alreadyAdded = FALSE;
/**
* Create an instance of ColorboxAttachment.
*/
public function __construct(ActivationCheckInterface $activation, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config) {
$this->activation = $activation;
$this->moduleHandler = $module_handler;
$this->settings = $config->get('colorbox.settings');;
}
/**
* {@inheritdoc}
*/
public function isApplicable() {
return !drupal_installation_attempted() && !static::$alreadyAdded && $this->activation->isActive();
}
/**
* {@inheritdoc}
*/
public function attach(array &$page) {
static::$alreadyAdded = TRUE;
if ($this->settings->get('custom.activate')) {
$js_settings = array(
'transition' => $this->settings->get('custom.transition_type'),
'speed' => $this->settings->get('custom.transition_speed'),
'opacity' => $this->settings->get('custom.opacity'),
'slideshow' => $this->settings->get('custom.slideshow.slideshow') ? TRUE : FALSE,
'slideshowAuto' => $this->settings->get('custom.slideshow.auto') ? TRUE : FALSE,
'slideshowSpeed' => $this->settings->get('custom.slideshow.speed'),
'slideshowStart' => $this->settings->get('custom.slideshow.text_start'),
'slideshowStop' => $this->settings->get('custom.slideshow.text_stop'),
'current' => $this->settings->get('custom.text_current'),
'previous' => $this->settings->get('custom.text_previous'),
'next' => $this->settings->get('custom.text_next'),
'close' => $this->settings->get('custom.text_close'),
'overlayClose' => $this->settings->get('custom.overlayclose') ? TRUE : FALSE,
'maxWidth' => $this->settings->get('custom.maxwidth'),
'maxHeight' => $this->settings->get('custom.maxheight'),
'initialWidth' => $this->settings->get('custom.initialwidth'),
'initialHeight' => $this->settings->get('custom.initialheight'),
'fixed' => $this->settings->get('custom.fixed') ? TRUE : FALSE,
'scrolling' => $this->settings->get('custom.scrolling') ? TRUE : FALSE,
'mobiledetect' => $this->settings->get('advanced.mobile_detect') ? TRUE : FALSE,
'mobiledevicewidth' => $this->settings->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' => $this->settings->get('advanced.mobile_detect') ? TRUE : FALSE,
'mobiledevicewidth' => $this->settings->get('advanced.mobile_device_width'),
);
}
$style = $this->settings->get('custom.style');
// Give other modules the possibility to override Colorbox settings and style.
$this->moduleHandler->alter('colorbox_settings', $js_settings, $style);
// Add colorbox js settings.
$page['#attached']['drupalSettings']['colorbox'] = $js_settings;
// Add and initialise the Colorbox plugin.
if ($this->settings->get('advanced.compression_type' == 'minified')) {
$page['#attached']['library'][] = 'colorbox/colorbox';
}
else {
$page['#attached']['library'][] = 'colorbox/colorbox-dev';
}
// Add JS and CSS based on selected style.
if ($style != 'none') {
$page['#attached']['library'][] = "colorbox/$style";
}
}
}
<?php
namespace Drupal\colorbox;
/**
* An interface for attaching things to the built page.
*/
interface PageAttachmentInterface {
/**
* Attach information to the page array.
*
* @param array $page
* The page array.
*/
public function attach(array &$page);
/**
* Check if the attachment should be added to the current page.
*
* @return bool
* TRUE if the attachment should be added to the current page.
*/
public function isApplicable();
}
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