UserRouteContext.php in Open Social 8.6
Same filename and directory in other branches
- 8.9 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 8 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 8.2 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 8.3 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 8.4 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 8.5 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 8.7 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 8.8 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 10.3.x modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 10.0.x modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 10.1.x modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
- 10.2.x modules/social_features/social_user/src/ContextProvider/UserRouteContext.php
Namespace
Drupal\social_user\ContextProviderFile
modules/social_features/social_user/src/ContextProvider/UserRouteContext.phpView source
<?php
namespace Drupal\social_user\ContextProvider;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Context\ContextProviderInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Plugin\Context\Context;
use Drupal\user\UserInterface;
/**
* Class UserRouteContext.
*
* @package Drupal\social_user\ContextProvider
*/
class UserRouteContext implements ContextProviderInterface {
use StringTranslationTrait;
/**
* The current route match object.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $currentRouteMatch;
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* Constructs a new UserRouteContext.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
* The current route match object.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager.
*/
public function __construct(RouteMatchInterface $current_route_match, EntityTypeManagerInterface $entity_type_manager) {
$this->currentRouteMatch = $current_route_match;
$this->userStorage = $entity_type_manager
->getStorage('user');
}
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids) {
// Create an optional context definition for user entities.
$context_definition = new ContextDefinition('entity:user', $this
->t('User from URL'), FALSE);
// Cache this context on the route.
$cacheability = new CacheableMetadata();
$cacheability
->setCacheContexts([
'route',
]);
// Create a context from the definition and retrieved user.
$context = new Context($context_definition, $this
->getUserFromRoute());
$context
->addCacheableDependency($cacheability);
return [
'user' => $context,
];
}
/**
* Retrieves the user entity from the current route.
*
* This will try to load the user entity from the route if present.
*
* @return \Drupal\user\UserInterface|null
* A user entity if one could be found, NULL otherwise.
*/
public function getUserFromRoute() {
$route_match = $this->currentRouteMatch;
// See if the route has a user parameter and try to retrieve it.
if ($account = $route_match
->getParameter('user')) {
if ($account instanceof UserInterface) {
return $account;
}
elseif (is_numeric($account)) {
$account = $this->userStorage
->load($account);
if ($account instanceof UserInterface) {
return $account;
}
}
}
}
/**
* {@inheritdoc}
*/
public function getAvailableContexts() {
return $this
->getRuntimeContexts([]);
}
}
Classes
Name | Description |
---|---|
UserRouteContext | Class UserRouteContext. |