You are here

class RedirectSubscriber in Open Social 8.7

Same name in this branch
  1. 8.7 modules/social_features/social_search/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_search\EventSubscriber\RedirectSubscriber
  2. 8.7 modules/social_features/social_group/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group\EventSubscriber\RedirectSubscriber
  3. 8.7 modules/social_features/social_user/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_user\EventSubscriber\RedirectSubscriber
  4. 8.7 modules/social_features/social_group/modules/social_group_quickjoin/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_quickjoin\EventSubscriber\RedirectSubscriber
  5. 8.7 modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber
  6. 8.7 modules/social_features/social_group/modules/social_group_flexible_group/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_flexible_group\EventSubscriber\RedirectSubscriber
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
  2. 8.5 modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber
  3. 8.6 modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber
  4. 8.8 modules/social_features/social_group/modules/social_group_default_route/src/EventSubscriber/RedirectSubscriber.php \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber
  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
  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
  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
  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

Class RedirectSubscriber.

@package Drupal\social_group_default_route\EventSubscriber

Hierarchy

  • class \Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RedirectSubscriber

1 string reference to 'RedirectSubscriber'
social_group_default_route.services.yml in modules/social_features/social_group/modules/social_group_default_route/social_group_default_route.services.yml
modules/social_features/social_group/modules/social_group_default_route/social_group_default_route.services.yml
1 service uses RedirectSubscriber
social_group_default_route.redirect_subscriber in modules/social_features/social_group/modules/social_group_default_route/social_group_default_route.services.yml
Drupal\social_group_default_route\EventSubscriber\RedirectSubscriber

File

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

Namespace

Drupal\social_group_default_route\EventSubscriber
View source
class RedirectSubscriber implements EventSubscriberInterface {

  /**
   * The current route.
   *
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected $currentRoute;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxy
   */
  protected $currentUser;

  /**
   * Redirectsubscriber construct.
   *
   * @param \Drupal\Core\Routing\CurrentRouteMatch $route_match
   *   The current route.
   * @param \Drupal\Core\Session\AccountProxy $current_user
   *   The current user.
   */
  public function __construct(CurrentRouteMatch $route_match, AccountProxy $current_user) {
    $this->currentRoute = $route_match;
    $this->currentUser = $current_user;
  }

  /**
   * Get the request events.
   *
   * @return mixed
   *   Returns request events.
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'groupLandingPage',
    ];
    return $events;
  }

  /**
   * This method is called when the KernelEvents::REQUEST event is dispatched.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The event.
   */
  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()));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RedirectSubscriber::$currentRoute protected property The current route.
RedirectSubscriber::$currentUser protected property The current user.
RedirectSubscriber::getSubscribedEvents public static function Get the request events.
RedirectSubscriber::groupLandingPage public function This method is called when the KernelEvents::REQUEST event is dispatched.
RedirectSubscriber::__construct public function Redirectsubscriber construct.