Commit b022668b authored by frjo's avatar frjo

Ported unique_token features from Drupal 7 version.

parent a1b1d10e
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Unicode;
/**
......@@ -22,6 +23,8 @@ use Drupal\Component\Utility\Unicode;
* - settings: Formatter settings array.
*/
function template_preprocess_colorbox_formatter(&$variables) {
static $gallery_token = NULL;
$item = $variables['item'];
$item_attributes = isset($variables['item_attributes']) ? $variables['item_attributes'] : array();
......@@ -71,7 +74,7 @@ function template_preprocess_colorbox_formatter(&$variables) {
$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')) && (Unicode::strlen($caption) > $trim_length)) {
$caption = drupal_substr($caption, 0, $trim_length - 5) . '...';
$caption = Unicode::substr($caption, 0, $trim_length - 5) . '...';
}
// Build the gallery id.
......@@ -100,6 +103,16 @@ function template_preprocess_colorbox_formatter(&$variables) {
$gallery_id = '';
}
// If gallery id is not empty add unique per-request token to avoid images being added manually to galleries.
if (!empty($gallery_id) && $config->get('advanced.unique_token')) {
// Check if gallery token has already been set, we need to reuse the token for the whole request.
if (is_null($gallery_token)) {
// We use a short token since randomness is not critical.
$gallery_token = Crypt::randomBytesBase64(8);
}
$gallery_id = $gallery_id . '-' . $gallery_token;
}
// Set up the $variables['image'] parameter.
if ($settings['style_first']) {
$variables['image'] = array(
......
......@@ -22,6 +22,7 @@ custom:
text_start: 'start slideshow'
text_stop: 'stop slideshow'
advanced:
unique_token: '1'
mobile_detect: '1'
mobile_device_width: '480px'
caption_trim: '0'
......
......@@ -74,6 +74,9 @@ colorbox.settings:
type: mapping
label: 'Advanced settings'
mapping:
unique_token:
type: integer
label: 'Unique per-request gallery token'
mobile_detect:
type: integer
label: 'Mobile detection'
......
......@@ -240,6 +240,13 @@ class ColorboxSettingsForm extends ConfigFormBase {
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['colorbox_advanced_settings']['colorbox_unique_token'] = array(
'#type' => 'radios',
'#title' => t('Unique per-request gallery token'),
'#options' => array(1 => t('On'), 0 => t('Off')),
'#default_value' => $config->get('advanced.unique_token'),
'#description' => t('If On (default), Colorbox will add a unique per-request token to the gallery id to avoid images being added manually to galleries. The token was added as a security fix but some see the old behavoiur as an feature and this settings makes it possible to remove the token.'),
);
$form['colorbox_advanced_settings']['colorbox_mobile_detect'] = array(
'#type' => 'radios',
'#title' => t('Mobile detection'),
......@@ -333,6 +340,7 @@ class ColorboxSettingsForm extends ConfigFormBase {
->set('custom.slideshow.speed', $form_state->getValue('colorbox_slideshowspeed'))
->set('custom.slideshow.text_start', $form_state->getValue('colorbox_text_start'))
->set('custom.slideshow.text_stop', $form_state->getValue('colorbox_text_stop'))
->set('advanced.unique_token', $form_state->getValue('colorbox_unique_token'))
->set('advanced.mobile_detect', $form_state->getValue('colorbox_mobile_detect'))
->set('advanced.mobile_detect_width', $form_state->getValue('colorbox_mobile_device_width'))
->set('advanced.caption_trim', $form_state->getValue('colorbox_caption_trim'))
......
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