You are here

public function PhotosAlbumController::albumView in Album Photos 8.5

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

Returns an overview of recent albums and photos.

Return value

array A render array.

File

src/Controller/PhotosAlbumController.php, line 176

Class

PhotosAlbumController
Album view controller.

Namespace

Drupal\photos\Controller

Code

public function albumView() {

  // @todo move to theme function and deprecate this in favor of default view.
  $config = $this
    ->config('photos.settings');

  // Get node object.
  $album = [];
  $node = $this->routeMatch
    ->getParameter('node');
  $nid = $node
    ->id();

  // Get order or set default order.
  $order = explode('|', isset($node->album['imageorder']) ? $node->album['imageorder'] : $config
    ->get('photos_display_imageorder'));
  $order = PhotosAlbum::orderValueChange($order[0], $order[1]);
  $limit = isset($node->album['viewpager']) ? $node->album['viewpager'] : $config
    ->get('photos_display_viewpager');
  $get_field = $this->requestStack
    ->getCurrentRequest()->query
    ->get('field');
  $get_sort = $this->requestStack
    ->getCurrentRequest()->query
    ->get('sort');
  $column = $get_field ? Html::escape($get_field) : '';
  $sort = isset($get_sort) ? Html::escape($get_sort) : '';
  $term = PhotosAlbum::orderValue($column, $sort, $limit, $order);

  // Album image's query.
  // @todo move to PhotosAlbum()->getImages().
  // @todo entity query?
  $query = $this->connection
    ->select('photos_image_field_data', 'p')
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');

  // @todo p.fid will fail.
  $query
    ->join('users_field_data', 'u', 'u.uid = p.uid');
  $query
    ->fields('p', [
    'id',
  ])
    ->condition('p.album_id', $nid)
    ->limit($term['limit']);
  if ($term['order']['column'] != 'p.id') {
    $query
      ->orderBy('p.id', 'DESC');
  }
  $results = $query
    ->execute();

  // Check comment settings.
  $com = $config
    ->get('photos_comment');

  // Check node access.
  $edit = $node
    ->access('update');
  $del = $node
    ->access('delete');
  $style_name = isset($node->album['list_imagesize']) ? $node->album['list_imagesize'] : $config
    ->get('photos_display_list_imagesize');

  // Necessary when upgrading from D6 to D7.
  // @todo fix in migration if needed?
  $image_styles = image_style_options(FALSE);
  if (!isset($image_styles[$style_name])) {
    $style_name = $config
      ->get('photos_display_list_imagesize');
  }

  // Process images.
  // @todo load multiple list view.
  // @todo use view for default album view.
  foreach ($results as $result) {

    // Load photos image.
    try {
      $photosImage = $this->entityTypeManager
        ->getStorage('photos_image')
        ->load($result->id);
    } catch (InvalidPluginDefinitionException $e) {
      watchdog_exception('photos', $e);
      throw new NotFoundHttpException();
    } catch (PluginNotFoundException $e) {
      watchdog_exception('photos', $e);
      throw new NotFoundHttpException();
    }
    $render_photos_image = $this->entityTypeManager
      ->getViewBuilder('photos_image')
      ->view($photosImage, 'full');
    $album['view'][] = $render_photos_image;
  }
  if (isset($album['view'][0])) {
    $album['access']['edit'] = $edit;

    // Node edit link.
    $url = Url::fromUri('base:node/' . $nid . '/edit');
    $album['node_edit_url'] = Link::fromTextAndUrl($this
      ->t('Album settings'), $url);

    // Image management link.
    $url = Url::fromUri('base:node/' . $nid . '/photos');
    $album['image_management_url'] = Link::fromTextAndUrl($this
      ->t('Upload photos'), $url);

    // Album URL.
    $album['album_url'] = Url::fromUri('base:photos/' . $nid)
      ->toString();
    $album['links'] = PhotosAlbum::orderLinks('photos/' . $nid, 0, 0, 1);
    $cover_style_name = $config
      ->get('photos_cover_imagesize');

    // Album cover view.
    if (isset($node->album['cover'])) {
      $album['cover'] = $node->album['cover'];
    }
    else {

      // @todo is this needed?
      $image_info = $this->imageFactory
        ->get($node->album['cover']['uri']);
      $title = $node
        ->getTitle();
      $album_cover_array = [
        '#theme' => 'image_style',
        '#style_name' => $cover_style_name,
        '#uri' => $node->album['cover']['uri'],
        '#width' => $image_info
          ->getWidth(),
        '#height' => $image_info
          ->getHeight(),
        '#alt' => $title,
        '#title' => $title,
        '#cache' => [
          'tags' => [
            'photos:album:' . $nid,
            'node:' . $nid,
          ],
        ],
      ];
      $album['cover'] = $album_cover_array;
    }
    $album['pager'] = [
      '#type' => 'pager',
    ];

    // Build album view.
    $album_view_array = [
      '#theme' => 'photos_album_view',
      '#album' => $album,
      '#node' => $node,
      '#cache' => [
        'tags' => [
          'photos:album:' . $nid,
          'node:' . $nid,
        ],
      ],
    ];
    $content = $album_view_array;
  }
  else {
    $content = [
      '#markup' => $this
        ->t('Album is empty'),
      '#cache' => [
        'tags' => [
          'photos:album:' . $nid,
          'node:' . $nid,
        ],
      ],
    ];
  }
  return $content;
}