GalleryIdHelper.php in Colorbox 8
File
src/GalleryIdHelper.php
View source
<?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 {
protected $galleryToken = NULL;
protected $configFactory;
protected $token;
public function __construct(ConfigFactoryInterface $configFactory, Token $token) {
$this->configFactory = $configFactory;
$this->token = $token;
}
public function generateId(ContentEntityInterface $entity, FieldItemInterface $item, array $settings) {
$entity_bundle = $entity
->bundle();
$entity_type = $entity
->getEntityTypeId();
$config = $this->configFactory
->get('colorbox.settings');
$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 (!empty($gallery_id) && $config
->get('advanced.unique_token')) {
if (is_null($this->galleryToken)) {
$this->galleryToken = Crypt::randomBytesBase64(8);
}
$gallery_id = $gallery_id . '-' . $this->galleryToken;
}
return $gallery_id;
}
}