Commit 026433f2 authored by Neslee Canil Pinto's avatar Neslee Canil Pinto Committed by Neslee Canil Pinto

Issue #2935266 by pasan.gamage, nitesh624, ilgnerfagundes, Neslee Canil Pinto:...

Issue #2935266 by pasan.gamage, nitesh624, ilgnerfagundes, Neslee Canil Pinto: Make gallery id generation function independent
parent c4c7e6ee
......@@ -5,3 +5,6 @@ services:
colorbox.attachment:
class: Drupal\colorbox\ColorboxAttachment
arguments: ['@colorbox.activation_check', '@module_handler', '@config.factory']
colorbox.gallery_id_generator:
class: Drupal\colorbox\GalleryIdHelper
arguments: ['@config.factory', '@token']
......@@ -5,7 +5,6 @@
* Colorbox theme functions.
*/
use Drupal\Component\Utility\Crypt;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
......@@ -88,48 +87,7 @@ function template_preprocess_colorbox_formatter(&$variables) {
$caption = mb_substr($caption, 0, $trim_length - 5) . '...';
}
// Build the gallery id.
$id = $entity->id();
$entity_id = !empty($id) ? $entity_bundle . '-' . $id : 'entity-id';
$field_name = $item->getParent()->getName();
switch ($settings['colorbox_gallery']) {
case 'post':
$gallery_id = 'gallery-' . $entity_id;
break;
case 'page':
$gallery_id = 'gallery-all';
break;
case 'field_post':
$gallery_id = 'gallery-' . $entity_id . '-' . $field_name;
break;
case 'field_page':
$gallery_id = 'gallery-' . $field_name;
break;
case 'custom':
$token_service = \Drupal::token();
$gallery_id = $token_service->replace($settings['colorbox_gallery_custom'], [$entity_type => $entity, 'file' => $item], ['clear' => TRUE]);
break;
default:
$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;
}
$gallery_id = \Drupal::service('colorbox.gallery_id_generator')->generateId($entity, $item, $settings);
// Set up the $variables['image'] parameter.
if ($settings['style_name'] == 'hide') {
......
<?php
namespace Drupal\colorbox;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Utility\Token;
/**
* Class GalleryIdHelper
*
* @package Drupal\colorbox
*/
class GalleryIdHelper {
/**
* @var null
*/
protected $galleryToken = NULL;
/**
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* GalleryIdHelper constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* @param \Drupal\Core\Utility\Token $token
*/
public function __construct(ConfigFactoryInterface $configFactory, Token $token) {
$this->configFactory = $configFactory;
$this->token = $token;
}
/**
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* @param \Drupal\Core\Field\FieldItemInterface $item
* @param array $settings
*
* @return string
*/
public function generateId(ContentEntityInterface $entity, FieldItemInterface $item, array $settings) {
$entity_bundle = $entity->bundle();
$entity_type = $entity->getEntityTypeId();
$config = $this->configFactory->get('colorbox.settings');
// Build the gallery id.
$id = $entity->id();
$entity_id = !empty($id) ? $entity_bundle . '-' . $id : 'entity-id';
$field_name = $item->getParent()->getName();
switch ($settings['colorbox_gallery']) {
case 'post':
$gallery_id = 'gallery-' . $entity_id;
break;
case 'page':
$gallery_id = 'gallery-all';
break;
case 'field_post':
$gallery_id = 'gallery-' . $entity_id . '-' . $field_name;
break;
case 'field_page':
$gallery_id = 'gallery-' . $field_name;
break;
case 'custom':
$gallery_id = $this->token->replace(
$settings['colorbox_gallery_custom'], [$entity_type => $entity,'file' => $item], ['clear' => TRUE]);
break;
default:
$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($this->galleryToken)) {
// We use a short token since randomness is not critical.
$this->galleryToken = Crypt::randomBytesBase64(8);
}
$gallery_id = $gallery_id . '-' . $this->galleryToken;
}
return $gallery_id;
}
}
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