You are here

class GroupRouteContext in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Context/GroupRouteContext.php \Drupal\group\Context\GroupRouteContext

Sets the current group as a context on group routes.

Hierarchy

Expanded class hierarchy of GroupRouteContext

1 string reference to 'GroupRouteContext'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses GroupRouteContext
group.group_route_context in ./group.services.yml
Drupal\group\Context\GroupRouteContext

File

src/Context/GroupRouteContext.php, line 18

Namespace

Drupal\group\Context
View source
class GroupRouteContext implements ContextProviderInterface {
  use GroupRouteContextTrait;
  use StringTranslationTrait;

  /**
   * Constructs a new GroupRouteContext.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
   *   The current route match object.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(RouteMatchInterface $current_route_match, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
    $this->currentRouteMatch = $current_route_match;
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {

    // Create an optional context definition for group entities.
    $context_definition = EntityContextDefinition::fromEntityTypeId('group')
      ->setRequired(FALSE);

    // Cache this context per group on the route.
    $cacheability = new CacheableMetadata();
    $cacheability
      ->setCacheContexts([
      'route.group',
    ]);

    // Create a context from the definition and retrieved or created group.
    $context = new Context($context_definition, $this
      ->getGroupFromRoute());
    $context
      ->addCacheableDependency($cacheability);
    return [
      'group' => $context,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    return [
      'group' => EntityContext::fromEntityTypeId('group', $this
        ->t('Group from URL')),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupRouteContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
GroupRouteContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
GroupRouteContext::__construct public function Constructs a new GroupRouteContext.
GroupRouteContextTrait::$currentRouteMatch protected property The current route match object.
GroupRouteContextTrait::$entityTypeManager protected property The entity type manager service.
GroupRouteContextTrait::getCurrentRouteMatch protected function Gets the current route match object.
GroupRouteContextTrait::getEntityTypeManager protected function Gets the entity type manager service.
GroupRouteContextTrait::getGroupFromRoute public function Retrieves the group entity from the current route.
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.