You are here

public function PhotosImage::load in Album Photos 8.5

Same name and namespace in other branches
  1. 8.4 src/PhotosImage.php \Drupal\photos\PhotosImage::load()

Load image file and album data.

1 call to PhotosImage::load()
PhotosImage::view in src/PhotosImage.php
Return render array to view image.

File

src/PhotosImage.php, line 35

Class

PhotosImage
Create images object.

Namespace

Drupal\photos

Code

public function load() {
  $fid = $this->fid;

  // Query image data.
  // @todo check access. Is ->addTag('node_access') needed here? If so, rewrite query.
  //   - I think access is already checked before we get here.
  $db = \Drupal::database();

  // @note currently legacy mode requires default field_image.
  $image = $db
    ->query('SELECT f.fid, f.uri, f.filemime, f.created, f.filename, n.title as node_title, a.data, u.uid, u.name, p.*
      FROM {file_managed} f
      INNER JOIN {photos_image__field_image} i ON i.field_image_target_id = f.fid
      INNER JOIN {photos_image_field_data} p ON p.revision_id = i.revision_id
      INNER JOIN {node_field_data} n ON p.album_id = n.nid
      INNER JOIN {photos_album} a ON a.album_id = n.nid
      INNER JOIN {users_field_data} u ON f.uid = u.uid
      WHERE i.field_image_target_id = :fid', [
    ':fid' => $fid,
  ])
    ->fetchObject();

  // Set image height and width.
  if (!isset($image->height) && isset($image->uri)) {

    // The image.factory service will check if our image is valid.
    $image_info = \Drupal::service('image.factory')
      ->get($image->uri);
    if ($image_info
      ->isValid()) {
      $image->width = $image_info
        ->getWidth();
      $image->height = $image_info
        ->getHeight();
    }
    else {
      $image->width = $image->height = NULL;
    }
  }
  return $image;
}