58a83c376193a3b4d8e029a057603499c22dae6b
[yaffs-website] / web / modules / contrib / front / src / EventSubscriber / FrontPageSubscriber.php
1 <?php
2
3 namespace Drupal\front_page\EventSubscriber;
4
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;
9
10 class FrontPageSubscriber implements EventSubscriberInterface {
11
12   public function initData(GetResponseEvent $event) {
13     global $base_path;
14
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()) {
18       return;
19     }
20     // Don't run when site is in maintenance mode.
21     if (\Drupal::state()->get('system.maintenance_mode')) {
22       return;
23     }
24
25     // Ignore non index.php requests (like cron).
26     if (!empty($_SERVER['SCRIPT_FILENAME']) && realpath(DRUPAL_ROOT . '/index.php') != realpath($_SERVER['SCRIPT_FILENAME'])) {
27       return;
28     }
29
30     $front_page = null;
31     $isFrontPage = \Drupal::service('path.matcher')->isFrontPage();
32     if (\Drupal::config('front_page.settings')->get('enable', '') && $isFrontPage) {
33
34
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'];
45         }
46       }
47     }
48
49     if ($front_page) {
50       $event->setResponse(new RedirectResponse($front_page));
51
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();
55     }
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   static function getSubscribedEvents() {
62     $events[KernelEvents::REQUEST][] = array('initData');
63     return $events;
64   }
65 }