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

Issue #2642142 by Sam152: Move the _colorbox_active check into a service

parent a0418395
......@@ -5,10 +5,6 @@
* A light-weight, customizable lightbox plugin for jQuery 1.3
*/
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Path\PathMatcher;
use Drupal\Core\Url;
/**
* The default path to the Colorbox directory.
*/
......@@ -42,7 +38,7 @@ function colorbox_page_attachments(array &$page) {
if ($already_added) {
return; // Don't add the JavaScript and CSS multiple times.
}
if (!_colorbox_active()) {
if (!\Drupal::service('colorbox.activation_check')->isActive()) {
return; // Don't add the JavaScript and CSS on specified paths.
}
......@@ -151,39 +147,6 @@ function colorbox_libraries_info() {
return $libraries;
}
/**
* Check if Colorbox should be active for the current URL.
*
* @return bool
* TRUE if Colorbox should be active for the current page.
*/
function _colorbox_active() {
// Make it possible deactivate Colorbox with
// parameter ?colorbox=no in the url.
if (isset($_GET['colorbox']) && $_GET['colorbox'] == 'no') {
return FALSE;
}
// Assume there are no matches until one is found.
$page_match = FALSE;
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$config = \Drupal::config('colorbox.settings');
$pages = Unicode::strtolower(_colorbox_array_to_string($config->get('advanced.pages')));
// Compare the lowercase path alias (if any) and internal path.
$path = \Drupal::service('path.current')->getPath();
$path_alias = Unicode::strtolower(\Drupal::service('path.alias_storage')->lookupPathAlias($path, 'en'));
$page_match = \Drupal::service('path.matcher')->matchPath($path_alias, $pages);
if ($path_alias != $path) {
$page_match = $page_match || \Drupal::service('path.matcher')->matchPath($path, $pages);
}
$page_match = $config->get('advanced.visibility') == 0 ? !$page_match : $page_match;
return $page_match;
}
/**
* Converts a text with lines (\n) into an array of lines.
*
......
services:
colorbox.activation_check:
class: Drupal\colorbox\ActivationCheck
arguments: ['@config.factory', '@path.matcher', '@path.current', '@path.alias_storage']
<?php
/**
* @file
* Contains Drupal\colorbox\ActivationCheck.
*/
namespace Drupal\colorbox;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\AliasStorageInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Path\CurrentPathStack;
/**
* Implementation of ActivationCheckInterface.
*/
class ActivationCheck implements ActivationCheckInterface {
/**
* The colorbox settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $settings;
/**
* A service to match paths.
*
* @var \Drupal\Core\Path\PathMatcherInterface
*/
protected $pathMatcher;
/**
* The current path.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected $currentPath;
/**
* Alias storage.
*
* @var \Drupal\Core\Path\AliasStorageInterface
*/
protected $aliasStorage;
/**
* Create an instace of ActivationCheck.
*/
public function __construct(ConfigFactoryInterface $config, PathMatcherInterface $path_matcher, CurrentPathStack $current_path, AliasStorageInterface $alias_storage) {
$this->settings = $config->get('colorbox.settings');
$this->pathMatcher = $path_matcher;
$this->currentPath = $current_path;
$this->aliasStorage = $alias_storage;
}
/**
* {@inheritdoc}
*/
public function isActive() {
// Make it possible deactivate Colorbox with
// parameter ?colorbox=no in the url.
if (isset($_GET['colorbox']) && $_GET['colorbox'] == 'no') {
return FALSE;
}
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = Unicode::strtolower(_colorbox_array_to_string($this->settings->get('advanced.pages')));
// Compare the lowercase path alias (if any) and internal path.
$path = $this->currentPath->getPath();
$path_alias = Unicode::strtolower($this->aliasStorage->lookupPathAlias($path, 'en'));
$page_match = $this->pathMatcher->matchPath($path_alias, $pages);
if ($path_alias != $path) {
$page_match = $page_match || $this->pathMatcher->matchPath($path, $pages);
}
$page_match = $this->settings->get('advanced.visibility') == 0 ? !$page_match : $page_match;
return $page_match;
}
}
<?php
/**
* @file
* Drupal\colorbox\ActivationCheckInterface.
*/
namespace Drupal\colorbox;
/**
* An interface for checking if colorbox should be active.
*/
interface ActivationCheckInterface {
/**
* Check if colorbox should be activated for the current page.
*
* @return bool
* If colorbox should be active.
*/
public function isActive();
}
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