ProfileHeroBlock.php in Open Social 8
Same filename and directory in other branches
- 8.9 modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 8.2 modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 8.3 modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 8.4 modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 8.5 modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 8.6 modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 8.7 modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 8.8 modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 10.3.x modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 10.0.x modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 10.1.x modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
- 10.2.x modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.php
Namespace
Drupal\social_profile\Plugin\BlockFile
modules/social_features/social_profile/src/Plugin/Block/ProfileHeroBlock.phpView source
<?php
namespace Drupal\social_profile\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a 'ProfileHeroBlock' block.
*
* @Block(
* id = "profile_hero_block",
* admin_label = @Translation("Profile hero block"),
* context = {
* "user" = @ContextDefinition("entity:user")
* }
* )
*/
class ProfileHeroBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* ProfileHeroBlock constructor.
*
* @param array $configuration
* The given configuration.
* @param string $plugin_id
* The given plugin id.
* @param mixed $plugin_definition
* The given plugin definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$account = $this
->getContextValue('user');
$storage = $this->entityTypeManager
->getStorage('profile');
$profile = $storage
->loadByUser($account, 'profile');
if ($profile) {
$build['content'] = $this->entityTypeManager
->getViewBuilder('profile')
->view($profile, 'hero');
$build['content']['#cache']['tags'] = $this
->getCacheTags();
$build['content']['#cache']['contexts'] = $this
->getCacheContexts();
}
return $build;
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), [
'user.permissions',
]);
}
}
Classes
Name | Description |
---|---|
ProfileHeroBlock | Provides a 'ProfileHeroBlock' block. |