function template_preprocess_photos_image in Album Photos 6.0.x
Same name and namespace in other branches
- 8.5 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 1298 - 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']);
}
}
$variables['legacy_comments'] = [];
// Legacy comments. Display comments from D7 / 8.x-4.x.
if ($variables['page'] && \Drupal::moduleHandler()
->moduleExists('comment') && \Drupal::database()
->schema()
->tableExists('photos_comment') && $photos_image
->hasField('field_image')) {
if ($fid = $photos_image->field_image->target_id) {
// Check if any comments are linked to this image file.
$cids = \Drupal::database()
->query("SELECT cid FROM {photos_comment} WHERE fid = :fid", [
':fid' => $fid,
])
->fetchCol();
$comment_storage = \Drupal::entityTypeManager()
->getStorage('comment');
$view_builder = \Drupal::entityTypeManager()
->getViewBuilder('comment');
foreach ($cids as $cid) {
$comment = $comment_storage
->load($cid);
$view_comment = $view_builder
->view($comment, 'default');
if ($comment) {
$variables['legacy_comments'][] = $view_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];
}
}