photoswipe.theme.inc in PhotoSwipe 8
Same filename and directory in other branches
Photoswipe theme preprocess functions.
File
photoswipe.theme.incView source
<?php
/**
* @file
* Photoswipe theme preprocess functions.
*/
use Drupal\Core\Language\Language;
use Drupal\image\Entity\ImageStyle;
/**
* Prepares variables for a Photoswipe image field formatter.
*
* @param array $variables
* An associative array containing:
* - item: An ImageItem object.
* - display_settings: optional image styles.
*
* @ingroup themeable
*/
function template_preprocess_photoswipe_image_formatter(array &$variables) {
$item = $variables['item'];
$settings = $variables['display_settings'];
$node = $item
->getParent()
->getParent()
->getValue();
$uri = $item->entity
->getFileUri();
$alt = !empty($item->alt) ? $item->alt : '';
$title = !empty($item->title) ? $item->title : '';
if (empty($alt) && !empty($item->field_file_image_alt_text[Language::LANGCODE_NOT_SPECIFIED])) {
$alt = $item->field_file_image_alt_text[Language::LANGCODE_NOT_SPECIFIED][0]['value'];
}
if (empty($title) && !empty($item->field_file_image_title_text[Language::LANGCODE_NOT_SPECIFIED])) {
$title = $item->field_file_image_title_text[Language::LANGCODE_NOT_SPECIFIED][0]['value'];
}
$image = [
'#theme' => 'image_style',
'#uri' => $uri,
'#alt' => $alt,
'#title' => $title,
'#attributes' => $item->_attributes,
'#style_name' => $settings['photoswipe_node_style'],
];
if (isset($variables['delta']) && $variables['delta'] === 0 && !empty($settings['photoswipe_node_style_first'])) {
$image['#style_name'] = $settings['photoswipe_node_style_first'];
}
// The image.factory service will check if our image is valid.
$image_file = \Drupal::service('image.factory')
->get($uri);
if ($image_file
->isValid()) {
$image_width = $image_file
->getWidth();
$image_height = $image_file
->getHeight();
}
else {
$image_width = $image_height = NULL;
}
$dimensions = [];
if (!empty($image_width) && !empty($image_height)) {
$image['#width'] = $dimensions['width'] = $image_width;
$image['#height'] = $dimensions['height'] = $image_height;
}
// Create the path to the image that will show in Photoswipe.
if ($style_name = $settings['photoswipe_image_style']) {
// Load the image style.
$style = ImageStyle::load($style_name);
// Fetch the Image style path from the Image URI.
$path = $style
->buildUrl($uri);
// Set the dimensions.
$style
->transformDimensions($dimensions, $uri);
}
else {
$path = file_create_url($uri);
}
// Render as a standard image if an image style is not given.
if (empty($image['#style_name'])) {
$image['#theme'] = 'image';
}
// Set Caption for this image.
if (isset($settings['photoswipe_caption'])) {
$caption_setting = $settings['photoswipe_caption'];
switch ($caption_setting) {
case 'alt':
$caption = $alt;
break;
case 'title':
$caption = $title;
break;
case 'node_title':
if (!empty($node->title)) {
$caption = $node->title->value;
}
else {
$caption = $alt;
}
break;
default:
$field_view = $node->{$caption_setting}
->view();
$field_view['#view_mode'] = $settings['photoswipe_view_mode'] ? $settings['photoswipe_view_mode'] : 'default';
$caption = render($field_view);
break;
}
}
else {
$caption = $alt;
}
$variables['image'] = $image;
$variables['path'] = $path;
$variables['attributes'] = [
'class' => 'photoswipe',
'data-size' => $dimensions['width'] . 'x' . $dimensions['height'],
'data-overlay-title' => $caption,
];
}
Functions
Name | Description |
---|---|
template_preprocess_photoswipe_image_formatter | Prepares variables for a Photoswipe image field formatter. |