You are here

public function RedirectHomepageSubscriber::checkForHomepageRedirect in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  2. 8 modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  3. 8.2 modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  4. 8.3 modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  5. 8.4 modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  6. 8.6 modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  7. 8.7 modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  8. 8.8 modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  9. 10.3.x modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  10. 10.0.x modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  11. 10.1.x modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()
  12. 10.2.x modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php \Drupal\alternative_frontpage\EventSubscriber\RedirectHomepageSubscriber::checkForHomepageRedirect()

This method is called whenever the request event is dispatched.

Parameters

\Symfony\Component\EventDispatcher\Event $event: Triggering event.

File

modules/custom/alternative_frontpage/src/EventSubscriber/RedirectHomepageSubscriber.php, line 82

Class

RedirectHomepageSubscriber
Class RedirectHomepageSubscriber.

Namespace

Drupal\alternative_frontpage\EventSubscriber

Code

public function checkForHomepageRedirect(Event $event) {

  // Make sure front page module is not run when using cli or doing install.
  if (PHP_SAPI === 'cli' || drupal_installation_attempted()) {
    return;
  }

  // Don't run when site is in maintenance mode.
  if (\Drupal::state()
    ->get('system.maintenance_mode')) {
    return;
  }

  // Ignore non index.php requests (like cron).
  if (!empty($_SERVER['SCRIPT_FILENAME']) && realpath(DRUPAL_ROOT . '/index.php') != realpath($_SERVER['SCRIPT_FILENAME'])) {
    return;
  }

  /** @var \Symfony\Component\HttpFoundation\Request $request */
  $request = $event
    ->getRequest();
  $request_path = $request
    ->getPathInfo();
  $frontpage_an = $this->siteSettings
    ->get('page.front');
  if ($request_path === $frontpage_an || $request_path === '/') {
    $frontpage_lu = $this->alternativeFrontpageSettings
      ->get('frontpage_for_authenticated_user');
    if ($frontpage_an === $frontpage_lu) {
      return;
    }
    if ($frontpage_lu && $this->currentUser
      ->isAuthenticated()) {
      $cache_contexts = new CacheableMetadata();
      $cache_contexts
        ->setCacheContexts([
        'user.roles:anonymous',
      ]);
      $response = new CacheableRedirectResponse($frontpage_lu);
      $response
        ->addCacheableDependency($cache_contexts);
      $event
        ->setResponse($response);
    }
  }
}