function template_preprocess_photos_image in Album Photos 8.5
Same name and namespace in other branches
- 6.0.x photos.module \template_preprocess_photos_image()
Prepares variables for photos image templates.
Default template: photos-image.html.twig.
Parameters
array $variables: An associative array containing:
- elements: An array of elements to display.
- photos_image: The photos_image object.
See also
\Drupal\Core\Field\BaseFieldDefinition::setDisplayConfigurable()
File
- ./
photos.module, line 1271 - Implementation of photos.module.
Code
function template_preprocess_photos_image(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['photos_image'] = $variables['elements']['#photos_image'];
/** @var \Drupal\photos\Entity\PhotosImage $photos_image */
$photos_image = $variables['photos_image'];
// Get the album node.
$album_node = $photos_image
->get('album_id')->entity;
$variables['photos_album_node'] = $album_node;
// Make name field available separately. Skip this custom preprocessing if
// the field display is configurable and skipping has been enabled.
// @todo https://www.drupal.org/project/drupal/issues/3015623
// Eventually delete this code and matching template lines. Using
// $variables['content'] is more flexible and consistent.
$skip_custom_preprocessing = $photos_image
->getEntityType()
->get('enable_base_field_custom_preprocess_skipping');
if (!$skip_custom_preprocessing || !$photos_image
->getFieldDefinition('title')
->isDisplayConfigurable('view')) {
$variables['label'] = $variables['elements']['title'];
unset($variables['elements']['title']);
}
try {
$variables['url'] = $photos_image
->toUrl('canonical')
->toString();
} catch (EntityMalformedException $e) {
watchdog_exception('photos', $e);
$variables['url'] = '';
}
$variables['page'] = $variables['view_mode'] == 'full' && photos_image_is_page($photos_image);
$variables['setToCover'] = '';
if (isset($variables['elements']['links'])) {
// Image pager.
if (isset($variables['elements']['links']['pager'])) {
if (isset($variables['elements']['links']['pager']['nextUrl'])) {
$variables['pager']['nextUrl'] = $variables['elements']['links']['pager']['nextUrl'];
}
if (isset($variables['elements']['links']['pager']['prevUrl'])) {
$variables['pager']['prevUrl'] = $variables['elements']['links']['pager']['prevUrl'];
}
unset($variables['elements']['links']['pager']);
}
// Set to cover link.
if (isset($variables['elements']['links']['cover'])) {
$variables['setToCover'] = $variables['elements']['links']['cover'];
unset($variables['elements']['links']['cover']);
}
// Comment count.
if (isset($variables['elements']['links']['comment'])) {
$variables['commentCount'] = $variables['elements']['links']['comment'];
unset($variables['elements']['links']['comment']);
}
}
// Image visit count.
$disableImageVisitCount = \Drupal::config('photos.settings')
->get('photos_image_count');
// @todo if not page display admin links: edit, delete, set as cover, etc.
// Helpful $content variable for templates.
$variables += [
'content' => [],
];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}