You are here

public function PhotosImage::getPager in Album Photos 8.5

Same name and namespace in other branches
  1. 6.0.x 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\Entity

Code

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.
  foreach ($results as $result) {
    $num++;
    $photosImage = \Drupal::entityTypeManager()
      ->getStorage('photos_image')
      ->load($result->id);

    // @todo new pager display view mode.
    $image_view = \Drupal::entityTypeManager()
      ->getViewBuilder('photos_image')
      ->view($photosImage, 'pager');
    if ($stop == 1) {
      $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 ($photosImage
      ->id() == $entity_id) {
      $pager['currentView'] = $image_view;
      $stop = 1;
    }
    else {
      $pager['prevView'] = $image_view;

      // Previous image.
      $pager['prevUrl'] = Url::fromRoute('entity.photos_image.canonical', [
        'node' => $result->album_id,
        'photos_image' => $photosImage
          ->id(),
      ])
        ->toString();
    }
    $pager['albumTitle'] = $result->title;
  }

  // @todo theme photos_pager with options for image and no-image.
  return $pager;
}