You are here

public function PhotosUserImages::build in Album Photos 8.4

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/PhotosUserImages.php, line 102

Class

PhotosUserImages
Provides a 'Photos user images' block.

Namespace

Drupal\photos\Plugin\Block

Code

public function build() {

  // Retrieve existing configuration for this block.
  // @todo migrate variables to block configuration.
  $config = $this
    ->getConfiguration();
  $count = isset($config['image_count']) ? $config['image_count'] : 10;

  // Check current path for args to find uid.
  $current_path = trim($this->routeMatch
    ->getRouteObject()
    ->getPath(), '/');
  $arg = explode('/', $current_path);
  if (isset($arg[1])) {
    if ($arg[0] == 'photos' && isset($arg[2])) {
      switch ($arg[1]) {
        case 'image':
          $fid = $this->routeMatch
            ->getRawParameter('file');
          $uid = $this->connection
            ->query('SELECT uid FROM {file_managed} WHERE fid = :fid', [
            ':fid' => $fid,
          ])
            ->fetchField();
          break;
        case 'user':
          $uid = $this->routeMatch
            ->getRawParameter('user');
      }
    }
    if ($arg[0] == 'node' && isset($arg[1])) {
      $nid = $this->routeMatch
        ->getRawParameter('node');
      $uid = $this->connection
        ->query('SELECT uid FROM {node_field_data} WHERE nid = :nid', [
        ':nid' => $nid,
      ])
        ->fetchField();
    }
  }
  if (!isset($uid)) {
    $uid = $this->account
      ->id();
  }
  if ($uid && ($block_info = PhotosImage::blockView('user', $count, 'photos/image', $uid))) {
    return [
      '#markup' => $block_info['content'],
      '#title' => $block_info['title'],
      '#cache' => [
        'tags' => [
          'photos:image:user:' . $uid,
          'user:' . $uid,
        ],
      ],
    ];
  }
}