You are here

public function SocialAlbumCountAndAddBlock::build in Open Social 10.1.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_album/src/Plugin/Block/SocialAlbumCountAndAddBlock.php \Drupal\social_album\Plugin\Block\SocialAlbumCountAndAddBlock::build()
  2. 10.0.x modules/social_features/social_album/src/Plugin/Block/SocialAlbumCountAndAddBlock.php \Drupal\social_album\Plugin\Block\SocialAlbumCountAndAddBlock::build()
  3. 10.2.x modules/social_features/social_album/src/Plugin/Block/SocialAlbumCountAndAddBlock.php \Drupal\social_album\Plugin\Block\SocialAlbumCountAndAddBlock::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

modules/social_features/social_album/src/Plugin/Block/SocialAlbumCountAndAddBlock.php, line 81

Class

SocialAlbumCountAndAddBlock
Provides a block to display images count and a button for adding new images.

Namespace

Drupal\social_album\Plugin\Block

Code

public function build() {
  $build = [];
  if (!($properties = $this
    ->getProperties())) {
    return $build;
  }
  $view = Views::getView('albums');
  $view
    ->setArguments([
    $this->routeMatch
      ->getRawParameter($properties['type']),
  ]);
  $view
    ->execute($properties['display']);
  $build['count'] = [
    '#markup' => $this
      ->formatPlural($view->total_rows, $properties['count']['singular'], $properties['count']['plural']),
  ];
  $url = Url::fromRoute($properties['link']['route']['name'], $properties['link']['route']['parameters'], [
    'attributes' => [
      'class' => [
        'btn',
        'btn-primary',
      ],
    ],
  ]);
  if ($url
    ->access()) {
    $build['link'] = Link::fromTextAndUrl($properties['link']['text'], $url)
      ->toRenderable();
  }
  return $build;
}