You are here

class UserRouteContext in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  2. 8.2 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  3. 8.3 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  4. 8.4 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  5. 8.5 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  6. 8.6 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  7. 8.7 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  8. 8.8 modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  9. 10.3.x modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  10. 10.0.x modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  11. 10.1.x modules/social_features/social_user/src/ContextProvider/UserRouteContext.php \Drupal\social_user\ContextProvider\UserRouteContext
  12. 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

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 19

Namespace

Drupal\social_user\ContextProvider
View 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 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([]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UserRouteContext::$currentRouteMatch protected property The current route match object.
UserRouteContext::$userStorage protected property The user storage.
UserRouteContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
UserRouteContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
UserRouteContext::getUserFromRoute public function Retrieves the user entity from the current route.
UserRouteContext::__construct public function Constructs a new UserRouteContext.