class UserRouteContext in Open Social 10.3.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 8 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 8.2 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 8.3 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 8.4 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 8.5 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 8.6 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 8.7 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 8.8 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 10.0.x modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 10.1.x modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
- 10.2.x modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
Class UserRouteContext.
@package Drupal\social_user\ContextProvider
Hierarchy
- class \Drupal\social_user\ContextProvider\UserRouteContext implements ContextProviderInterface uses StringTranslationTrait
Expanded class hierarchy of UserRouteContext
1 string reference to 'UserRouteContext'
- social_user.services.yml in modules/
social_features/ social_user/ social_user.services.yml - modules/social_features/social_user/social_user.services.yml
1 service uses UserRouteContext
- social_user.user_route_context in modules/
social_features/ social_user/ social_user.services.yml - Drupal\social_user\ContextProvider\UserRouteContext
File
- modules/
social_features/ social_user/ src/ ContextProvider/ UserRouteContext.php, line 20
Namespace
Drupal\social_user\ContextProviderView source
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 group entities.
$context_definition = EntityContextDefinition::fromEntityTypeId('user')
->setRequired(FALSE);
// Cache this context per group on the route.
$cacheability = new CacheableMetadata();
$cacheability
->setCacheContexts([
'route',
]);
// Create a context from the definition and retrieved or created group.
$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 [
'social_user' => EntityContext::fromEntityTypeId('user', $this
->t('Social User entity from URL')),
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UserRouteContext:: |
protected | property | The current route match object. | |
UserRouteContext:: |
protected | property | The user storage. | |
UserRouteContext:: |
public | function |
Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface:: |
|
UserRouteContext:: |
public | function |
Gets runtime context values for the given context IDs. Overrides ContextProviderInterface:: |
|
UserRouteContext:: |
public | function | Retrieves the user entity from the current route. | |
UserRouteContext:: |
public | function | Constructs a new UserRouteContext. |