You are here

public function PhotosInformation::build in Album Photos 8.5

Same name and namespace in other branches
  1. 8.4 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 115

Class

PhotosInformation
Provides a 'Photo information' block.

Namespace

Drupal\photos\Plugin\Block

Code

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

  // Check which pager to load.
  $photosImage = $this->routeMatch
    ->getParameter('photos_image');
  $pager_type = 'album_id';
  if ($photosImage) {

    // Get current image.
    $query = $this->connection
      ->select('photos_image_field_data', 'p');
    $query
      ->join('node_field_data', 'n', 'n.nid = p.album_id');
    $query
      ->join('users_field_data', 'u', 'p.uid = u.uid');
    $query
      ->fields('p')
      ->fields('n', [
      'nid',
      'title',
    ])
      ->fields('u', [
      'name',
      'uid',
    ])
      ->condition('p.id', $photosImage
      ->id());
    $query
      ->addTag('node_access');
    $image = $query
      ->execute()
      ->fetchObject();
    $blockImage = [];
    if ($image) {

      /* @var \Drupal\user\UserInterface $account */
      try {
        $account = $this->entityTypeManager
          ->getStorage('user')
          ->load($image->uid);
        $blockImage['name'] = $account
          ->getDisplayName();
      } catch (InvalidPluginDefinitionException $e) {
        watchdog_exception('photos', $e);
      } catch (PluginNotFoundException $e) {
        watchdog_exception('photos', $e);
      }
      $blockImage['photos_image'] = $photosImage;
      $pager_id = $image->nid;

      // Get pager image(s).
      $blockImage['pager'] = $photosImage
        ->getPager($pager_id, $pager_type);
      $content = [
        '#theme' => 'photos_image_block',
        '#image' => $blockImage,
        '#cache' => [
          'tags' => [
            'photos:image:' . $photosImage
              ->id(),
            'photos:album:' . $image->nid,
            'node:' . $image->nid,
          ],
        ],
      ];
      $content['#attached']['library'][] = 'photos/photos.block.information';
    }
  }
  return $content;
}