086e35fb8e9eee4d06a13a172e8c4ca766d51a2f
[yaffs-website] / web / modules / contrib / front / src / EventSubscriber / FrontPageSubscriber.php
1 <?php
2
3 namespace Drupal\front_page\EventSubscriber;
4
5 use Drupal\Core\Url;
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;
10
11 /**
12  * Class FrontPageSubscriber.
13  *
14  * @package Drupal\front_page\EventSubscriber
15  */
16 class FrontPageSubscriber implements EventSubscriberInterface {
17
18   /**
19    * Manage the logic.
20    *
21    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
22    *   Managed event.
23    */
24   public function initData(GetResponseEvent $event) {
25     global $base_path;
26
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()) {
30       return;
31     }
32
33     // Don't run when site is in maintenance mode.
34     if (\Drupal::state()->get('system.maintenance_mode')) {
35       return;
36     }
37
38     // Ignore non index.php requests (like cron).
39     if (!empty($_SERVER['SCRIPT_FILENAME']) && realpath(DRUPAL_ROOT . '/index.php') != realpath($_SERVER['SCRIPT_FILENAME'])) {
40       return;
41     }
42
43     $front_page = NULL;
44     $isFrontPage = \Drupal::service('path.matcher')->isFrontPage();
45     if (\Drupal::config('front_page.settings')->get('enable', '') && $isFrontPage) {
46
47
48       $roles = \Drupal::currentUser()->getRoles();
49       $config = \Drupal::configFactory()->get('front_page.settings');
50       $current_weigth = NULL;
51
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)) {
56
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'];
61         }
62       }
63     }
64
65     if ($front_page) {
66       $current_language = \Drupal::languageManager()->getCurrentLanguage();
67       $url = Url::fromUserInput($front_page, ['language' => $current_language]);
68       $event->setResponse(new RedirectResponse($url->toString()));
69
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();
73     }
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   static function getSubscribedEvents() {
80     $events[KernelEvents::REQUEST][] = ['initData'];
81     return $events;
82   }
83 }