You are here

public static function PhotosImage::blockView in Album Photos 8.4

Extends image block view(s).

4 calls to PhotosImage::blockView()
PhotosController::contentOverview in src/Controller/PhotosController.php
Returns an overview of recent albums and photos.
PhotosRandomImages::build in src/Plugin/Block/PhotosRandomImages.php
Builds and returns the renderable array for this block plugin.
PhotosRecentImages::build in src/Plugin/Block/PhotosRecentImages.php
Builds and returns the renderable array for this block plugin.
PhotosUserImages::build in src/Plugin/Block/PhotosUserImages.php
Builds and returns the renderable array for this block plugin.

File

src/PhotosImage.php, line 484

Class

PhotosImage
Create images object.

Namespace

Drupal\photos

Code

public static function blockView($type, $limit, $url = 'photos/image', $uid = 0, $sort = [
  'column' => 'f.fid',
  'order' => 'DESC',
]) {
  $db = \Drupal::database();
  $query = $db
    ->select('file_managed', 'f');
  $query
    ->join('photos_image', 'p', 'p.fid = f.fid');
  $query
    ->join('node_field_data', 'n', 'n.nid = p.pid');
  $query
    ->join('users_field_data', 'u', 'u.uid = f.uid');
  $query
    ->fields('f', [
    'fid',
  ]);
  $query
    ->condition('n.status', 1);
  if ($type == 'user') {
    $query
      ->condition('f.uid', $uid);
  }
  if ($type == 'rand') {
    $query
      ->orderRandom();
  }
  else {
    $query
      ->orderBy($sort['column'], $sort['order']);
  }
  $query
    ->range(0, $limit);
  $query
    ->addTag('node_access');
  $results = $query
    ->execute();
  $images = [];
  $cache_tags = [];
  foreach ($results as $result) {
    $photos_image = new PhotosImage($result->fid);
    $image = $photos_image
      ->load();
    $image->href = Url::fromUri('base:' . $url . '/' . $image->fid);
    $images[] = $image;
    $cache_tags[] = 'photos:image:' . $image->fid;
  }
  if (isset($images[0]->fid)) {
    $render_array = [
      '#theme' => 'photos_block',
      '#images' => $images,
      '#block_type' => 'image',
      '#cache' => [
        'tags' => $cache_tags,
      ],
    ];
    $content = \Drupal::service('renderer')
      ->render($render_array);
    if ($url && count($images) >= $limit) {
      $more_link = [
        '#type' => 'more_link',
        '#url' => Url::fromUri('base:' . $url),
        '#title' => t('View more'),
      ];
      $content .= \Drupal::service('renderer')
        ->render($more_link);
    }
    if ($type == 'user') {
      return [
        'content' => $content,
        'title' => t("@name's images", [
          '@name' => $images[0]->name,
        ]),
      ];
    }
    else {
      return $content;
    }
  }
  return FALSE;
}