public function PhotosImage::getPager in Album Photos 6.0.x
Same name and namespace in other branches
- 8.5 src/Entity/PhotosImage.php \Drupal\photos\Entity\PhotosImage::getPager()
Gets the page for this image.
Parameters
int $id: The pager id: album_id or uid.
string $type: The type of pager: album_id or uid.
Return value
array The photos image pager data or render array.
Overrides PhotosImageInterface::getPager
File
- src/
Entity/ PhotosImage.php, line 485
Class
- PhotosImage
- Defines the photos image entity class.
Namespace
Drupal\photos\EntityCode
public function getPager($id, $type = 'album_id') {
$entity_id = $this
->id();
$db = \Drupal::database();
$query = $db
->select('photos_image_field_data', 'p');
$query
->innerJoin('node_field_data', 'n', 'n.nid = p.album_id');
$query
->fields('p', [
'id',
'album_id',
]);
$query
->fields('n', [
'title',
]);
// Default order by id.
$order = [
'column' => 'p.id',
'sort' => 'DESC',
];
if ($type == 'album_id') {
// Viewing album.
// Order images by album settings.
$album_data = $db
->query('SELECT data FROM {photos_album} WHERE album_id = :album_id', [
':album_id' => $id,
])
->fetchField();
// @todo look into core serialization API.
// @see https://www.drupal.org/docs/8/api/serialization-api/serialization-api-overview
$album_data = unserialize($album_data);
$default_order = \Drupal::config('photos.settings')
->get('photos_display_imageorder');
$image_order = isset($album_data['imageorder']) ? $album_data['imageorder'] : $default_order;
$order = explode('|', $image_order);
$order = PhotosAlbum::orderValueChange($order[0], $order[1]);
$query
->condition('p.album_id', $id);
}
elseif ($type == 'uid') {
// Viewing all user images.
$query
->condition('p.uid', $id);
}
$query
->orderBy($order['column'], $order['sort']);
if ($order['column'] != 'p.id') {
$query
->orderBy('p.id', 'DESC');
}
$results = $query
->execute();
$stop = $pager['prev'] = $pager['next'] = 0;
$num = 0;
// @todo use view mode.
$previousImageId = NULL;
$photosImageStorage = \Drupal::entityTypeManager()
->getStorage('photos_image');
$photosImageViewBuilder = \Drupal::entityTypeManager()
->getViewBuilder('photos_image');
foreach ($results as $result) {
$num++;
// @todo new pager display view mode.
if ($stop == 1) {
$photosImage = $photosImageStorage
->load($result->id);
$image_view = $photosImageViewBuilder
->view($photosImage, 'pager');
$pager['nextView'] = $image_view;
// Next image.
$pager['nextUrl'] = Url::fromRoute('entity.photos_image.canonical', [
'node' => $result->album_id,
'photos_image' => $photosImage
->id(),
])
->toString();
break;
}
if ($result->id == $entity_id) {
$photosImage = $photosImageStorage
->load($result->id);
$image_view = $photosImageViewBuilder
->view($photosImage, 'pager');
$pager['currentView'] = $image_view;
$stop = 1;
}
else {
$previousImageId = $result->id;
}
$pager['albumTitle'] = $result->title;
}
if ($previousImageId) {
$photosImage = $photosImageStorage
->load($previousImageId);
$image_view = $photosImageViewBuilder
->view($photosImage, 'pager');
$pager['prevView'] = $image_view;
// Previous image.
$pager['prevUrl'] = Url::fromRoute('entity.photos_image.canonical', [
'node' => $id,
'photos_image' => $photosImage
->id(),
])
->toString();
}
// @todo theme photos_pager with options for image and no-image.
return $pager;
}