You are here

public function PhotosAlbum::getCover in Album Photos 8.4

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

Get album cover.

File

src/PhotosAlbum.php, line 219

Class

PhotosAlbum
Create an album object.

Namespace

Drupal\photos

Code

public function getCover($fid = NULL, $title = '') {
  $pid = $this->pid;
  $cover = [];
  $image = FALSE;
  if (!$fid) {

    // Check album for cover fid.
    $db = \Drupal::database();
    $fid = $db
      ->query("SELECT fid FROM {photos_album} WHERE pid = :pid", [
      ':pid' => $pid,
    ])
      ->fetchField();
  }
  if ($fid) {

    // Load image.
    $photos_image = new PhotosImage($fid);
    $image = $photos_image
      ->load();
    if ($image) {

      // Prepare node album cover data.
      $cover['fid'] = $fid;
      $cover['uri'] = $image->uri;
      $style_name = \Drupal::config('photos.settings')
        ->get('photos_cover_imagesize') ?: 'thumbnail';
      $image->href = Url::fromUri('base:photos/album/' . $pid)
        ->toString();

      // Set alt and title to album node title.
      $image->alt = $title;
      $image->title = $title;
      $cover_view = [
        '#theme' => 'photos_image_html',
        '#image' => $image,
        '#style_name' => $style_name,
      ];
      $cover['view'] = $cover_view;
    }
  }
  if (!$fid || !$image) {

    // Cover not set, select an image from the album.
    $db = \Drupal::database();
    $query = $db
      ->select('file_managed', 'f');
    $query
      ->join('photos_image', 'p', 'p.fid = f.fid');
    $query
      ->fields('f', [
      'fid',
    ]);
    $query
      ->condition('p.pid', $pid);
    $fid = $query
      ->execute()
      ->fetchField();
    if ($fid) {
      return $this
        ->getCover($fid);
    }
  }
  return $cover;
}