View source
<?php
namespace Drupal\photoswipe;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Utility\Token;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PhotoswipePreprocessProcessor implements ContainerInjectionInterface {
protected $entityTypeManager;
protected $imageFactory;
protected $token;
protected $languageManager;
protected $renderer;
protected $logger;
protected $imageDTO;
public function __construct(EntityTypeManagerInterface $entityTypeManager, ImageFactory $imageFactory, Token $token, LanguageManagerInterface $languageManager, LoggerChannelFactoryInterface $channelFactory, RendererInterface $renderer) {
$this->entityTypeManager = $entityTypeManager;
$this->imageFactory = $imageFactory;
$this->token = $token;
$this->languageManager = $languageManager;
$this->renderer = $renderer;
$this->logger = $channelFactory
->get('photoswipe');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('image.factory'), $container
->get('token'), $container
->get('language_manager'), $container
->get('logger.factory'), $container
->get('renderer'));
}
public function preprocess(array &$variables) {
$this->imageDTO = ImageDTO::createFromVariables($variables);
$image = $this
->getRandarableImage($variables);
$this
->setDimensions($image);
$variables['image'] = $image;
$variables['path'] = $this
->getPath();
$variables['attributes']['class'][] = 'photoswipe';
$variables['attributes']['data-size'] = $this->imageDTO
->getWidth() . 'x' . $this->imageDTO
->getHeight();
$variables['attributes']['data-overlay-title'] = $this
->getCaption();
if (isset($image['#style_name']) && $image['#style_name'] === 'hide') {
$variables['attributes']['class'][] = 'hidden';
}
}
protected function getCaption() {
$settings = $this->imageDTO
->getSettings();
if (isset($settings['photoswipe_caption'])) {
$caption_setting = $settings['photoswipe_caption'];
switch ($caption_setting) {
case 'alt':
$caption = $this->imageDTO
->getAlt();
break;
case 'title':
$caption = $this->imageDTO
->getTitle();
break;
case 'node_title':
if (!empty($this->entity->title)) {
$caption = $this->imageDTO
->getEntity()->title->value;
}
else {
$caption = $this->imageDTO
->getAlt();
}
break;
case 'custom':
$entity_type = $this->imageDTO
->getEntity()
->getEntityTypeId();
$caption = $this->token
->replace($settings['photoswipe_caption_custom'], [
$entity_type => $this->imageDTO
->getEntity(),
'file' => $this->imageDTO
->getItem(),
], [
'clear' => TRUE,
'langcode' => $this->languageManager
->getCurrentLanguage()
->getId(),
]);
break;
default:
$field_view['#view_mode'] = $settings['photoswipe_view_mode'] ? $settings['photoswipe_view_mode'] : 'default';
if (!isset($entity->{$caption_setting})) {
$id = $this->imageDTO
->getEntity()
->id();
$msg = "'Photoswipe Caption' is unset for field view '@fv' on node: @nid.";
$this->logger
->warning($msg, [
'@fv' => $field_view['#view_mode'],
'@nid' => $id,
]);
$caption = $this->imageDTO
->getAlt();
break;
}
$field_view = $entity->{$caption_setting}
->view();
$caption = render($field_view);
break;
}
}
else {
$caption = $this->imageDTO
->getAlt();
}
return $caption;
}
protected function getRandarableImage(array $variables) {
$image = [
'#theme' => 'image_style',
'#uri' => $this->imageDTO
->getUri(),
'#alt' => $this->imageDTO
->getAlt(),
'#title' => $this->imageDTO
->getTitle(),
'#attributes' => $this->imageDTO
->getItem()->_attributes,
'#style_name' => $this->imageDTO
->getSettings()['photoswipe_node_style'],
];
if (isset($variables['delta']) && $variables['delta'] === 0 && !empty($this->imageDTO
->getSettings()['photoswipe_node_style_first'])) {
$image['#style_name'] = $this->imageDTO
->getSettings()['photoswipe_node_style_first'];
}
if (empty($image['#style_name']) || $image['#style_name'] === 'hide') {
$image['#theme'] = 'image';
}
return $image;
}
protected function getPath() {
$dimensions = $this->imageDTO
->getDimensions();
if (($style_name = $this->imageDTO
->getSettings()['photoswipe_image_style']) && !empty($dimensions)) {
$style = $this->entityTypeManager
->getStorage('image_style')
->load($style_name);
$style
->transformDimensions($dimensions, $this->imageDTO
->getUri());
$this->imageDTO
->setDimensions($dimensions);
return $style
->buildUrl($this->imageDTO
->getUri());
}
else {
return file_create_url($this->imageDTO
->getUri());
}
}
protected function setDimensions(array &$image) {
$image_file = $this->imageFactory
->get($this->imageDTO
->getUri());
if ($image_file
->isValid()) {
$image['#width'] = $image_file
->getWidth();
$image['#height'] = $image_file
->getHeight();
$this->imageDTO
->setDimensions([
ImageDTO::HEIGHT => $image_file
->getHeight(),
ImageDTO::WIDTH => $image_file
->getWidth(),
]);
}
}
}