You are here

public function PhotosRearrangeController::getAlbums in Album Photos 8.5

Same name and namespace in other branches
  1. 8.4 src/Controller/PhotosRearrangeController.php \Drupal\photos\Controller\PhotosRearrangeController::getAlbums()
  2. 6.0.x src/Controller/PhotosRearrangeController.php \Drupal\photos\Controller\PhotosRearrangeController::getAlbums()

Get user albums.

Parameters

int $uid: The user ID.

Return value

array An array of albums to rearrange.

1 call to PhotosRearrangeController::getAlbums()
PhotosRearrangeController::albumRearrange in src/Controller/PhotosRearrangeController.php
Rearrange user albums.

File

src/Controller/PhotosRearrangeController.php, line 269

Class

PhotosRearrangeController
Re-arrange view controller.

Namespace

Drupal\photos\Controller

Code

public function getAlbums($uid) {
  $albums = [];
  $get_limit = $this->requestStack
    ->getCurrentRequest()->query
    ->get('limit');
  $limit = $get_limit ? Html::escape($get_limit) : 50;
  $query = $this->connection
    ->select('node_field_data', 'n');
  $query
    ->join('photos_album', 'p', 'p.album_id = n.nid');
  $query
    ->fields('n', [
    'nid',
    'title',
  ]);
  $query
    ->fields('p', [
    'weight',
    'cover_id',
    'count',
  ]);
  $query
    ->condition('n.uid', $uid);
  $query
    ->range(0, $limit);
  $query
    ->orderBy('p.weight', 'ASC');
  $query
    ->orderBy('n.nid', 'DESC');
  $result = $query
    ->execute();
  foreach ($result as $data) {
    if (isset($data->cover_id) && $data->cover_id != 0) {
      $cover_id = $data->cover_id;
    }
    else {
      $cover_id = $this->connection
        ->query("SELECT id FROM {photos_image_field_data} WHERE album_id = :album_id", [
        ':album_id' => $data->nid,
      ])
        ->fetchField();
      if (empty($cover_id)) {

        // Skip albums with no images.
        continue;
      }
    }
    $albums[] = [
      'weight' => $data->weight,
      'nid' => $data->nid,
      'cover_id' => $cover_id,
      'count' => $data->count,
      'title' => $data->title,
    ];
  }
  return $albums;
}