You are here

public function PhotosAlbumController::listView in Album Photos 8.5

Same name and namespace in other branches
  1. 6.0.x src/Controller/PhotosAlbumController.php \Drupal\photos\Controller\PhotosAlbumController::listView()

Returns content for recent albums.

Return value

array An array containing markup for the page content.

File

src/Controller/PhotosAlbumController.php, line 321

Class

PhotosAlbumController
Album view controller.

Namespace

Drupal\photos\Controller

Code

public function listView() {

  // @todo convert this to a theme function for photos_album_photo_list field.
  $build = [
    '#cache' => [
      'tags' => [],
    ],
  ];

  // @todo a lot of duplicate code can be consolidated in these controllers.
  $query = $this->connection
    ->select('node', 'n')
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
  $query
    ->join('photos_album', 'p', 'p.album_id = n.nid');
  $query
    ->fields('n', [
    'nid',
  ]);
  $query
    ->orderBy('n.nid', 'DESC');
  $query
    ->limit(10);
  $query
    ->addTag('node_access');
  $results = $query
    ->execute();
  $build['photos_albums'] = [];

  // Check the setting for album list node view mode.
  $view_mode = $this
    ->config('photos.settings')
    ->get('view_mode_album_list_page');
  if (!$view_mode) {
    $view_mode = 'teaser';
  }
  foreach ($results as $result) {
    $node = $this->entityTypeManager
      ->getStorage('node')
      ->load($result->nid);
    $node_view = $this->entityTypeManager
      ->getViewBuilder('node')
      ->view($node, $view_mode);
    $build['photos_albums'][] = $node_view;
    $build['#cache']['tags'][] = 'node:' . $node
      ->id();
    $build['#cache']['tags'][] = 'photos:album:' . $node
      ->id();
  }
  if (!empty($build['photos_albums'])) {
    $build['#cache']['tags'][] = 'node_list';
    $build['pager'] = [
      '#type' => 'pager',
    ];
  }
  else {
    $build['photos_albums'][] = [
      '#markup' => $this
        ->t('No albums have been created yet.'),
    ];
  }
  return $build;
}