You are here

public function EventSubscribers::customRedirect in Open Social 8.9

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_group/modules/social_group_invite/src/EventSubscriber/EventSubscribers.php \Drupal\social_group_invite\EventSubscriber\EventSubscribers::customRedirect()
  2. 10.0.x modules/social_features/social_group/modules/social_group_invite/src/EventSubscriber/EventSubscribers.php \Drupal\social_group_invite\EventSubscriber\EventSubscribers::customRedirect()
  3. 10.1.x modules/social_features/social_group/modules/social_group_invite/src/EventSubscriber/EventSubscribers.php \Drupal\social_group_invite\EventSubscriber\EventSubscribers::customRedirect()
  4. 10.2.x modules/social_features/social_group/modules/social_group_invite/src/EventSubscriber/EventSubscribers.php \Drupal\social_group_invite\EventSubscriber\EventSubscribers::customRedirect()

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_invite/src/EventSubscriber/EventSubscribers.php, line 92

Class

EventSubscribers
Class CustomRedirects.

Namespace

Drupal\social_group_invite\EventSubscriber

Code

public function customRedirect(GetResponseEvent $event) {

  // First check if the current route is the group canonical.
  $routeMatch = $this->currentRoute
    ->getRouteName();
  $routes_to_check = [
    'view.group_invitations.page_1',
    'view.my_invitations.page_1',
  ];

  // Not related to group invite, we leave.
  if (!in_array($routeMatch, $routes_to_check, TRUE)) {
    return;
  }
  $url = NULL;

  // For the user group invite overview, we need the current user
  // to be a LU in order to be able to build the URL.
  if ($routeMatch === 'view.my_invitations.page_1') {

    // Check if user is logged in.
    if ($this->currentUser
      ->isAnonymous()) {
      return;
    }

    // Determine the URL we want to redirect to.
    $url = Url::fromRoute('view.social_group_user_invitations.page_1', [
      'user' => $this->currentUser
        ->id(),
    ]);
  }

  // For the group invites overview, we need the group
  // in order to be able to build the URL.
  if ($routeMatch === 'view.group_invitations.page_1') {

    // 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;
    }
    $url = Url::fromRoute('view.social_group_invitations.page_1', [
      'group' => $group
        ->id(),
    ]);
  }

  // If the current user has no access we leave it be.
  if (NULL !== $url && $url
    ->access($this->currentUser) === FALSE) {

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

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