You are here

public function PhotosAlbum::getImages in Album Photos 8.4

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

Get album images.

File

src/PhotosAlbum.php, line 286

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.wid',
    'sort' => 'asc',
  ]);

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

  // Prepare images.
  foreach ($results as $result) {
    $photos_image = new PhotosImage($result->fid);
    $images[] = $photos_image
      ->load();
  }
  if (isset($images[0]->fid)) {
    $node = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->load($this->pid);
    $images[0]->info = [
      'pid' => $node
        ->id(),
      'title' => $node
        ->getTitle(),
      'uid' => $node
        ->getOwnerId(),
    ];
    if (isset($node->album['cover'])) {
      $images[0]->info['cover'] = $node->album['cover'];
    }
  }
  return $images;
}