Explorar o código

refactored domain detection edlp_mobile.module

Bachir Soussi Chiadmi %!s(int64=6) %!d(string=hai) anos
pai
achega
7386ce404e
Modificáronse 1 ficheiros con 57 adicións e 54 borrados
  1. 57 54
      sites/all/modules/figli/edlp_mobile/edlp_mobile.module

+ 57 - 54
sites/all/modules/figli/edlp_mobile/edlp_mobile.module

@@ -58,69 +58,72 @@ function edlp_mobile_page_attachments(array &$attachments) {
   $is_mobile_domain = false;
 
   global $base_root;
-  //dpm($base_root, '$base_root');
+  // dpm($base_root, '$base_root');
 
-  $current_domain = preg_replace('/http:\/\/|https:\/\//i', '', $base_root);
-  //dpm($current_domain, '$current_domain');
+  $current_hostname = preg_replace('/http:\/\/|https:\/\//i', '', $base_root);
+  // dpm($current_hostname, '$current_hostname');
 
   $domain_storage = \Drupal::entityTypeManager()->getStorage('domain');
-  $domains = $domain_storage->loadMultiple();
-  //dpm($domains);
-  $domain_alias_storage = \Drupal::entityTypeManager()->getStorage('domain_alias');
-  //dpm($domain_alias_storage);
-
-  foreach ($domains as $domain_id => $domain) {
-    if($domain->isActive()){
-      //dpm($domain);
-
-      $alias_ids = $domain_alias_storage->getQuery()->condition('domain_id', $domain_id)->execute();
-      //dpm($alias_ids, 'alias_ids');
-
-      $aliases = $domain_alias_storage->loadMultiple($alias_ids);
-      // dpm($aliases, 'aliases');
-
-      foreach ($aliases as $alias_id => $alias) {
-        if($alias->getPattern() == $current_domain){
-          // we found the current domain and current alias
-          // dpm($domain, 'domain');
-          // dpm($alias, 'alias');
-          $env = $alias->getEnvironment();
-          $env_aliases = $domain_alias_storage->loadByEnvironment($env);
-          // dpm($env_aliases, '$env_aliases');
-          // find out mibile alias and desktop alias
-          // this is dirty
-          // TODO: set target device as domain setting
-          if ($domain->id() == 'm_encyclopediedelaparole_org'){
-            // current domain/alias is mobile
-            $is_mobile_domain = true;
-            $mobile_url = $alias->getPattern();
-            // find desktop pattern
-            foreach ($env_aliases as $env_alias_id => $env_alias) {
-              if($env_alias_id != $alias_id){
-                // we found the desktop pattern
-                $desktop_url = $env_alias->getPattern();
-              }
-            }
-          }else{
-            // current domain/alias is mobile
-            $is_mobile_domain = false;
-            $desktop_url = $alias->getPattern();
-            // find desktop pattern
-            foreach ($env_aliases as $env_alias_id => $env_alias) {
-              if($env_alias_id != $alias_id){
-                // we found the desktop pattern
-                $mobile_url = $env_alias->getPattern();
-              }
-            }
-          }
+
+  $mobile_domain = $domain_storage->load('m_encyclopediedelaparole_org');
+  // dpm($mobile_domain, 'mobile_domain');
+
+  $desktop_domain = $domain_storage->load('encyclopediedelaparole_org');
+  // dpm($desktop_domain, 'desktop_domain');
+
+  $current_domain = $domain_storage->loadByHostname($current_hostname);
+  // dpm($current_domain, 'current_domain');
+
+  if($current_domain){
+    // we are in a main domain
+    $is_mobile_domain = ($current_domain->id() == $mobile_domain->id());
+    $mobile_url = $mobile_domain->getHostname();
+    $desktop_url = $desktop_domain->getHostname();
+  }else{
+    // we are not in a main domain, so we look for an alias
+    $domain_alias_storage = \Drupal::entityTypeManager()->getStorage('domain_alias');
+    $alias = $domain_alias_storage->loadByHostname($current_hostname);
+    // dpm($alias, '$alias');
+    $domain_id = $alias->getDomainId();
+    // dpm($domain_id, 'domain_id');
+    $current_domain = $domain_storage->load($domain_id);
+    // dpm($current_domain, 'current_domain');
+
+    $env = $alias->getEnvironment();
+    $env_aliases = $domain_alias_storage->loadByEnvironment($env);
+    // dpm($env_aliases, '$env_aliases');
+    // find out mibile alias and desktop alias
+    // this is dirty
+    // TODO: set target device as domain setting
+    if ($domain_id == 'm_encyclopediedelaparole_org'){
+      // current domain/alias is mobile
+      $is_mobile_domain = true;
+      $mobile_url = $alias->getPattern();
+      // find desktop pattern
+      // we assume that there is only two alias by env (mobile or desktop)
+      foreach ($env_aliases as $env_alias_id => $env_alias) {
+        if($env_alias_id != $alias->id()){
+          // we found the desktop pattern
+          $desktop_url = $env_alias->getPattern();
+          break;
+        }
+      }
+    }else{
+      // current domain/alias is desktop
+      $is_mobile_domain = false;
+      $desktop_url = $alias->getPattern();
+      // find mobile pattern
+      // we assume that there is only two alias by env (mobile or desktop)
+      foreach ($env_aliases as $env_alias_id => $env_alias) {
+        if($env_alias_id != $alias->id()){
+          // we found the desktop pattern
+          $mobile_url = $env_alias->getPattern();
           break;
         }
       }
-      break;
     }
   }
 
-  // $mobile =
   $current_path = \Drupal::service('path.current')->getPath();
   $is_front = \Drupal::service('path.matcher')->isFrontPage();
   $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();