3 namespace Drupal\front_page\EventSubscriber;
6 use Symfony\Component\HttpFoundation\RedirectResponse;
7 use Symfony\Component\HttpKernel\KernelEvents;
8 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12 * Class FrontPageSubscriber.
14 * @package Drupal\front_page\EventSubscriber
16 class FrontPageSubscriber implements EventSubscriberInterface {
21 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
24 public function initData(GetResponseEvent $event) {
27 // Make sure front page module is not run when using cli (drush).
28 // Make sure front page module does not run when installing Drupal either.
29 if (PHP_SAPI === 'cli' || drupal_installation_attempted()) {
33 // Don't run when site is in maintenance mode.
34 if (\Drupal::state()->get('system.maintenance_mode')) {
38 // Ignore non index.php requests (like cron).
39 if (!empty($_SERVER['SCRIPT_FILENAME']) && realpath(DRUPAL_ROOT . '/index.php') != realpath($_SERVER['SCRIPT_FILENAME'])) {
44 $isFrontPage = \Drupal::service('path.matcher')->isFrontPage();
45 if (\Drupal::config('front_page.settings')->get('enable', '') && $isFrontPage) {
48 $roles = \Drupal::currentUser()->getRoles();
49 $config = \Drupal::configFactory()->get('front_page.settings');
50 $current_weigth = NULL;
52 foreach ($roles as $role) {
53 $role_config = $config->get('rid_' . $role);
54 if ((isset($role_config['enabled']) && $role_config['enabled'] == TRUE)
55 && (($role_config['weigth'] < $current_weigth) || $current_weigth === NULL)) {
57 // $base_path can contain a / at the end, strip to avoid double slash.
58 $path = rtrim($base_path, '/');
59 $front_page = $path . $role_config['path'];
60 $current_weigth = $role_config['weigth'];
66 $current_language = \Drupal::languageManager()->getCurrentLanguage();
67 $url = Url::fromUserInput($front_page, ['language' => $current_language]);
68 $event->setResponse(new RedirectResponse($url->toString()));
70 // @todo Probably we must to remove this and manage cache by role.
71 // Turn caching off for this page as it is dependant on role.
72 \Drupal::service('page_cache_kill_switch')->trigger();
79 static function getSubscribedEvents() {
80 $events[KernelEvents::REQUEST][] = ['initData'];