Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / front / src / EventSubscriber / FrontPageSubscriber.php
index 58a83c376193a3b4d8e029a057603499c22dae6b..086e35fb8e9eee4d06a13a172e8c4ca766d51a2f 100644 (file)
@@ -2,21 +2,34 @@
 
 namespace Drupal\front_page\EventSubscriber;
 
+use Drupal\Core\Url;
 use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\HttpKernel\KernelEvents;
 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
+/**
+ * Class FrontPageSubscriber.
+ *
+ * @package Drupal\front_page\EventSubscriber
+ */
 class FrontPageSubscriber implements EventSubscriberInterface {
 
+  /**
+   * Manage the logic.
+   *
+   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
+   *   Managed event.
+   */
   public function initData(GetResponseEvent $event) {
     global $base_path;
 
     // Make sure front page module is not run when using cli (drush).
-    // Make sur front page module does not run when installing Drupal either.
+    // Make sure front page module does not run when installing Drupal either.
     if (PHP_SAPI === 'cli' || drupal_installation_attempted()) {
       return;
     }
+
     // Don't run when site is in maintenance mode.
     if (\Drupal::state()->get('system.maintenance_mode')) {
       return;
@@ -27,18 +40,21 @@ class FrontPageSubscriber implements EventSubscriberInterface {
       return;
     }
 
-    $front_page = null;
+    $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;
+      $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.
+        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.
           $path = rtrim($base_path, '/');
           $front_page = $path . $role_config['path'];
           $current_weigth = $role_config['weigth'];
@@ -47,9 +63,11 @@ class FrontPageSubscriber implements EventSubscriberInterface {
     }
 
     if ($front_page) {
-      $event->setResponse(new RedirectResponse($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.
+      // @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();
     }
@@ -59,7 +77,7 @@ class FrontPageSubscriber implements EventSubscriberInterface {
    * {@inheritdoc}
    */
   static function getSubscribedEvents() {
-    $events[KernelEvents::REQUEST][] = array('initData');
+    $events[KernelEvents::REQUEST][] = ['initData'];
     return $events;
   }
 }