You are here

class FrontPageSubscriber in Front Page 8

Same name and namespace in other branches
  1. 9.1.x src/EventSubscriber/FrontPageSubscriber.php \Drupal\front_page\EventSubscriber\FrontPageSubscriber

Class FrontPageSubscriber.

@package Drupal\front_page\EventSubscriber

Hierarchy

  • class \Drupal\front_page\EventSubscriber\FrontPageSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of FrontPageSubscriber

1 string reference to 'FrontPageSubscriber'
front_page.services.yml in ./front_page.services.yml
front_page.services.yml
1 service uses FrontPageSubscriber
front_page.event_subscriber in ./front_page.services.yml
Drupal\front_page\EventSubscriber\FrontPageSubscriber

File

src/EventSubscriber/FrontPageSubscriber.php, line 17

Namespace

Drupal\front_page\EventSubscriber
View source
class FrontPageSubscriber implements EventSubscriberInterface {

  /**
   * Manage the logic.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   Managed event.
   */
  public function initData(GetResponseEvent $event) {

    // Make sure front page module is not run when using cli (drush).
    // Make sure front page module does not run when installing Drupal either.
    if (PHP_SAPI === 'cli' || InstallerKernel::installationAttempted()) {
      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;
    }
    $front_page = NULL;
    $isFrontPage = \Drupal::service('path.matcher')
      ->isFrontPage();
    if (\Drupal::config('front_page.settings')
      ->get('enable', '') && $isFrontPage) {
      $roles = \Drupal::currentUser()
        ->getRoles();
      $config = \Drupal::configFactory()
        ->get('front_page.settings');
      $current_weigth = NULL;
      foreach ($roles as $role) {
        $role_config = $config
          ->get('rid_' . $role);
        if (isset($role_config['enabled']) && $role_config['enabled'] == TRUE && ($role_config['weigth'] < $current_weigth || $current_weigth === NULL)) {

          // $base_path can contain a / at the end, strip to avoid double slash.
          $front_page = $role_config['path'];
          $current_weigth = $role_config['weigth'];
        }
      }
    }
    if ($front_page) {

      // Add '/' to the beginning of url if url not begin with with a '/', '?', or '#'.
      if (strpos($front_page, '/') !== 0 && strpos($front_page, '#') !== 0 && strpos($front_page, '?') !== 0) {
        $front_page = "/{$front_page}";
      }
      $current_language = \Drupal::languageManager()
        ->getCurrentLanguage();
      $url = Url::fromUserInput($front_page, [
        'language' => $current_language,
      ]);
      $event
        ->setResponse(new RedirectResponse($url
        ->toString()));

      // @todo Probably we must to remove this and manage cache by role.
      // Turn caching off for this page as it is dependant on role.
      \Drupal::service('page_cache_kill_switch')
        ->trigger();
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'initData',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FrontPageSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
FrontPageSubscriber::initData public function Manage the logic.