You are here

public function PhotosAlbum::getImages in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 src/PhotosAlbum.php \Drupal\photos\PhotosAlbum::getImages()
  2. 8.4 src/PhotosAlbum.php \Drupal\photos\PhotosAlbum::getImages()

Get album images.

File

src/PhotosAlbum.php, line 295

Class

PhotosAlbum
Create an album object.

Namespace

Drupal\photos

Code

public function getImages($limit = 10) {
  $images = [];

  // Prepare query.
  $get_field = \Drupal::request()->query
    ->get('field');
  $column = $get_field ? Html::escape($get_field) : '';
  $get_sort = \Drupal::request()->query
    ->get('sort');
  $sort = $get_sort ? Html::escape($get_sort) : '';
  $term = PhotosAlbum::orderValue($column, $sort, $limit, [
    'column' => 'p.weight',
    'sort' => 'asc',
  ]);

  // Query images in this album.
  $db = \Drupal::database();
  $query = $db
    ->select('photos_image_field_data', 'p')
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
  $query
    ->join('users_field_data', 'u', 'p.uid = u.uid');
  $query
    ->join('node_field_data', 'n', 'n.nid = p.album_id');
  $query
    ->fields('p', [
    'id',
  ]);
  $query
    ->condition('p.album_id', $this->albumId);
  $query
    ->limit($term['limit']);
  $query
    ->orderBy($term['order']['column'], $term['order']['sort']);
  if ($term['order']['column'] != 'f.fid') {
    $query
      ->orderBy('p.id', 'DESC');
  }

  // $query->addTag('node_access');
  $results = $query
    ->execute();

  // Prepare images.
  foreach ($results as $result) {
    $photosImage = \Drupal::entityTypeManager()
      ->getStorage('photos_image')
      ->load($result->id);
    $images[] = [
      'photos_image' => $photosImage,
    ];
  }
  return $images;
}