View source
<?php
namespace Drupal\social_album\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialAlbumCountAndAddBlock extends BlockBase implements ContainerFactoryPluginInterface {
const ITEM = [
'count' => [
'singular' => '@count album',
'plural' => '@count albums',
],
];
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_route_match'));
}
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;
}
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIf($this
->getProperties() !== NULL);
}
public function getCacheContexts() {
$cache_contexts = parent::getCacheContexts();
if ($this
->getProperties()) {
$cache_contexts = Cache::mergeContexts($cache_contexts, [
'url',
]);
}
return $cache_contexts;
}
public function getCacheTags() {
$tags = parent::getCacheTags();
if ($properties = $this
->getProperties()) {
$type = $properties['type'];
$tags = Cache::mergeTags($tags, [
$type . ':' . $this->routeMatch
->getRawParameter($type),
]);
}
return $tags;
}
protected function getProperties() {
$items = [
'entity.node.canonical' => [
'type' => 'node',
'display' => 'embed_album_overview',
'count' => [
'singular' => '@count image',
'plural' => '@count images',
],
'link' => [
'text' => $this
->t('Add images'),
'route' => [
'name' => 'social_album.post',
'parameters' => [
'node' => $this->routeMatch
->getRawParameter('node'),
],
],
],
],
'view.albums.page_albums_overview' => [
'type' => 'user',
'display' => 'page_albums_overview',
'link' => [
'text' => $this
->t('Create new album'),
'route' => [
'name' => 'node.add',
'parameters' => [
'node_type' => 'album',
],
],
],
] + self::ITEM,
'view.albums.page_group_albums_overview' => [
'type' => 'group',
'display' => 'page_group_albums_overview',
'link' => [
'text' => $this
->t('Create new album'),
'route' => [
'name' => 'social_album.add',
'parameters' => [
'group' => $this->routeMatch
->getRawParameter('group'),
],
],
],
] + self::ITEM,
];
return $items[$this->routeMatch
->getRouteName()] ?? NULL;
}
}