You are here

public function PhotosInformation::build in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/Block/PhotosInformation.php \Drupal\photos\Plugin\Block\PhotosInformation::build()
  2. 6.0.x src/Plugin/Block/PhotosInformation.php \Drupal\photos\Plugin\Block\PhotosInformation::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/PhotosInformation.php, line 102

Class

PhotosInformation
Provides a 'Photo information' block.

Namespace

Drupal\photos\Plugin\Block

Code

public function build() {
  $content = [];

  // Check which pager to load.
  $fid = $this->routeMatch
    ->getParameter('file');
  $current_path = trim($this->routeMatch
    ->getRouteObject()
    ->getPath(), '/');
  $arg = explode('/', $current_path);
  $pager_type = 'pid';
  if (isset($arg[2])) {

    // @todo cleanup sub-albums were removed.
    $get_photos_sub = $this->requestStack
      ->getCurrentRequest()->query
      ->get('photos_sub');
    if ($arg[0] == 'photos' && $arg[1] == 'image' && !empty($fid) && $get_photos_sub) {
      $pager_type = 'sub';
      $pager_id = (int) $get_photos_sub;
    }
    elseif ($arg[0] == 'photos' && $arg[1] == 'image' && !empty($fid)) {
      $pager_type = 'pid';
    }
  }
  if (isset($fid) && !empty($fid)) {

    // Get current image.
    $query = $this->connection
      ->select('photos_image', 'p');
    $query
      ->join('file_managed', 'f', 'f.fid = p.fid');
    $query
      ->join('node_field_data', 'n', 'n.nid = p.pid');
    $query
      ->join('users_field_data', 'u', 'f.uid = u.uid');
    $query
      ->fields('p', [
      'count',
      'comcount',
      'exif',
      'des',
    ])
      ->fields('f', [
      'uri',
      'created',
      'filemime',
      'fid',
    ])
      ->fields('n', [
      'nid',
      'title',
    ])
      ->fields('u', [
      'name',
      'uid',
    ])
      ->condition('p.fid', $fid);
    $query
      ->addTag('node_access');
    $image = $query
      ->execute()
      ->fetchObject();
    if ($image) {
      if ($pager_type == 'pid') {
        $pager_id = $image->nid;
      }

      // Get pager image(s).
      $photos_image = new PhotosImage($fid);
      $image->pager = $photos_image
        ->pager($pager_id, $pager_type);
      $content = [
        '#theme' => 'photos_image_block',
        '#image' => $image,
        '#cache' => [
          'max_age' => 0,
        ],
      ];
      $content['#attached']['library'][] = 'photos/photos.block.information';
    }
    return $content;
  }
}