You are here

public function RedirectSubscriber::groupLandingPage in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber::groupLandingPage()
  2. 8.5 modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber::groupLandingPage()
  3. 8.6 modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber::groupLandingPage()
  4. 8.7 modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber::groupLandingPage()
  5. 10.3.x modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber::groupLandingPage()
  6. 10.0.x modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber::groupLandingPage()
  7. 10.1.x modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber::groupLandingPage()
  8. 10.2.x modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber::groupLandingPage()

This method is called when the KernelEvents::REQUEST event is dispatched.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseEvent $event: The event.

File

modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php, line 66

Class

RedirectSubscriber
Class RedirectSubscriber.

Namespace

Drupal\social_group_default_route\EventSubscriber

Code

public function groupLandingPage(GetResponseEvent $event) {

  // First check if the current route is the group canonical.
  $routeMatch = $this->currentRoute
    ->getRouteName();

  // Not group canonical, then we leave.
  if ($routeMatch !== 'entity.group.canonical') {
    return;
  }

  // Fetch the group parameter and check if's an actual group.
  $group = $this->currentRoute
    ->getParameter('group');

  // Not group, then we leave.
  if (!$group instanceof Group) {
    return;
  }

  // Set the already default redirect route.
  $defaultRoute = 'social_group.stream';
  $defaultClosedRoute = 'view.group_information.page_group_about';

  // Check if this group has a custom route set.
  $route = $group
    ->getFieldValue('default_route', 'value');

  // Check if current user is a member.
  if ($group
    ->getMember(User::load($this->currentUser
    ->id())) === FALSE) {
    $route = $group
      ->getFieldValue('default_route_an', 'value');

    // If you're not a member and the group type is closed.
    if ($route === NULL) {
      $route = $group
        ->getGroupType()
        ->id() === 'closed_group' ? $defaultClosedRoute : $defaultRoute;
    }
  }

  // Still no route here? Then we use the normal default.
  if ($route === NULL) {
    $route = $defaultRoute;
  }

  // Determine the URL we want to redirect to.
  $url = Url::fromRoute($route, [
    'group' => $group
      ->id(),
  ]);

  // If it's not set, set to canonical, or the current user has no access.
  if (!isset($route) || $route === $routeMatch || $url
    ->access($this->currentUser) === FALSE) {

    // This basically means that the normal flow remains intact.
    return;
  }

  // Redirect.
  $event
    ->setResponse(new RedirectResponse($url
    ->toString()));
}