GroupStatisticBlock.php in Open Social 10.1.x
File
modules/social_features/social_group/src/Plugin/Block/GroupStatisticBlock.php
View source
<?php
namespace Drupal\social_group\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\GroupInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class GroupStatisticBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $routeMatch;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
$this->entityTypeManager = $entityTypeManager;
}
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'), $container
->get('entity_type.manager'));
}
public function build() {
$build = [];
$group = _social_group_get_current_group();
if (!empty($group)) {
$content = $this->entityTypeManager
->getViewBuilder('group')
->view($group, 'statistic');
$build['content'] = $content;
$build['#cache']['tags'][] = 'group_block:' . $group
->id();
}
return $build;
}
protected function blockAccess(AccountInterface $account) {
if (theme_get_setting('style') === 'sky') {
return AccessResult::allowed();
}
return AccessResult::forbidden();
}
public function getCacheTags() {
$group = _social_group_get_current_group();
if (!$group instanceof GroupInterface) {
return parent::getCacheContexts();
}
return Cache::mergeTags(parent::getCacheTags(), [
'group_content_list:entity:' . \Drupal::currentUser()
->id(),
'group_content_list:plugin:group_invitation:entity:' . \Drupal::currentUser()
->id(),
'group:' . $group
->id(),
]);
}
public function getCacheContexts() {
$cache_contexts = parent::getCacheContexts();
$cache_contexts[] = 'user';
$cache_contexts[] = 'group';
$cache_contexts[] = 'route.group';
$cache_contexts[] = 'url';
return $cache_contexts;
}
public function getCacheMaxAge() {
return 0;
}
}