3 namespace Drupal\front_page\EventSubscriber;
5 use Symfony\Component\HttpFoundation\RedirectResponse;
6 use Symfony\Component\HttpKernel\KernelEvents;
7 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10 class FrontPageSubscriber implements EventSubscriberInterface {
12 public function initData(GetResponseEvent $event) {
15 // Make sure front page module is not run when using cli (drush).
16 // Make sur front page module does not run when installing Drupal either.
17 if (PHP_SAPI === 'cli' || drupal_installation_attempted()) {
20 // Don't run when site is in maintenance mode.
21 if (\Drupal::state()->get('system.maintenance_mode')) {
25 // Ignore non index.php requests (like cron).
26 if (!empty($_SERVER['SCRIPT_FILENAME']) && realpath(DRUPAL_ROOT . '/index.php') != realpath($_SERVER['SCRIPT_FILENAME'])) {
31 $isFrontPage = \Drupal::service('path.matcher')->isFrontPage();
32 if (\Drupal::config('front_page.settings')->get('enable', '') && $isFrontPage) {
35 $roles = \Drupal::currentUser()->getRoles();
36 $config = \Drupal::configFactory()->get('front_page.settings');
37 $current_weigth = null;
38 foreach ($roles as $role) {
39 $role_config = $config->get('rid_' . $role);
40 if((isset($role_config['enabled']) && $role_config['enabled'] == true) && (($role_config['weigth'] < $current_weigth) || $current_weigth === null)) {
41 //$base_path can contain a / at the end, strip to avoid double slash.
42 $path = rtrim($base_path, '/');
43 $front_page = $path . $role_config['path'];
44 $current_weigth = $role_config['weigth'];
50 $event->setResponse(new RedirectResponse($front_page));
52 //@todo Probably we must to remove this and manage cache by role.
53 // Turn caching off for this page as it is dependant on role.
54 \Drupal::service('page_cache_kill_switch')->trigger();
61 static function getSubscribedEvents() {
62 $events[KernelEvents::REQUEST][] = array('initData');