You are here

public function GroupRouteContextTrait::getGroupFromRoute in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Context/GroupRouteContextTrait.php \Drupal\group\Context\GroupRouteContextTrait::getGroupFromRoute()

Retrieves the group entity from the current route.

This will try to load the group entity from the route if present. If we are on the group add form, it will return a new group entity with the group type set.

Return value

\Drupal\group\Entity\GroupInterface|null A group entity if one could be found or created, NULL otherwise.

2 calls to GroupRouteContextTrait::getGroupFromRoute()
GroupRouteContext::getRuntimeContexts in src/Context/GroupRouteContext.php
Gets runtime context values for the given context IDs.
RouteGroupCacheContext::getContext in src/Cache/Context/RouteGroupCacheContext.php
Returns the string representation of the cache context.

File

src/Context/GroupRouteContextTrait.php, line 68

Class

GroupRouteContextTrait
Trait to get the group entity from the current route.

Namespace

Drupal\group\Context

Code

public function getGroupFromRoute() {
  $route_match = $this
    ->getCurrentRouteMatch();

  // See if the route has a group parameter and try to retrieve it.
  if (($group = $route_match
    ->getParameter('group')) && $group instanceof GroupInterface) {
    return $group;
  }
  elseif ($route_match
    ->getRouteName() == 'entity.group.add_form') {
    $group_type = $route_match
      ->getParameter('group_type');
    return $this
      ->getEntityTypeManager()
      ->getStorage('group')
      ->create([
      'type' => $group_type
        ->id(),
    ]);
  }
  return NULL;
}