ProfileStatisticBlock.php in Open Social 8.8
File
modules/social_features/social_profile/src/Plugin/Block/ProfileStatisticBlock.php
View source
<?php
namespace Drupal\social_profile\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProfileStatisticBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$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('entity_type.manager'));
}
public function build() {
$build = [];
$account = $this
->getContextValue('user');
$storage = $this->entityTypeManager
->getStorage('profile');
$profile = $storage
->loadByUser($account, 'profile');
if ($profile) {
$build = $this->entityTypeManager
->getViewBuilder('profile')
->view($profile, 'statistic');
}
return $build;
}
public function getCacheTags() {
$account = $this
->getContextValue('user');
$storage = $this->entityTypeManager
->getStorage('profile');
$profile = $storage
->loadByUser($account, 'profile');
$tags = [
'user:' . $account
->id(),
];
if ($profile) {
$tags[] = 'profile:' . $profile
->id();
}
return Cache::mergeTags(parent::getCacheTags(), $tags);
}
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), [
'user.permissions',
]);
}
protected function blockAccess(AccountInterface $account) {
if (theme_get_setting('style') === 'sky') {
return AccessResult::allowed();
}
return AccessResult::forbidden();
}
}