refactored domain detection edlp_mobile.module

This commit is contained in:
2018-04-26 23:20:50 +02:00
parent b6d90554a6
commit 7386ce404e
@@ -58,69 +58,72 @@ function edlp_mobile_page_attachments(array &$attachments) {
$is_mobile_domain = false; $is_mobile_domain = false;
global $base_root; global $base_root;
//dpm($base_root, '$base_root'); // dpm($base_root, '$base_root');
$current_domain = preg_replace('/http:\/\/|https:\/\//i', '', $base_root); $current_hostname = preg_replace('/http:\/\/|https:\/\//i', '', $base_root);
//dpm($current_domain, '$current_domain'); // dpm($current_hostname, '$current_hostname');
$domain_storage = \Drupal::entityTypeManager()->getStorage('domain'); $domain_storage = \Drupal::entityTypeManager()->getStorage('domain');
$domains = $domain_storage->loadMultiple();
//dpm($domains); $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'); $domain_alias_storage = \Drupal::entityTypeManager()->getStorage('domain_alias');
//dpm($domain_alias_storage); $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');
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 = $alias->getEnvironment();
$env_aliases = $domain_alias_storage->loadByEnvironment($env); $env_aliases = $domain_alias_storage->loadByEnvironment($env);
// dpm($env_aliases, '$env_aliases'); // dpm($env_aliases, '$env_aliases');
// find out mibile alias and desktop alias // find out mibile alias and desktop alias
// this is dirty // this is dirty
// TODO: set target device as domain setting // TODO: set target device as domain setting
if ($domain->id() == 'm_encyclopediedelaparole_org'){ if ($domain_id == 'm_encyclopediedelaparole_org'){
// current domain/alias is mobile // current domain/alias is mobile
$is_mobile_domain = true; $is_mobile_domain = true;
$mobile_url = $alias->getPattern(); $mobile_url = $alias->getPattern();
// find desktop pattern // 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) { foreach ($env_aliases as $env_alias_id => $env_alias) {
if($env_alias_id != $alias_id){ if($env_alias_id != $alias->id()){
// we found the desktop pattern // we found the desktop pattern
$desktop_url = $env_alias->getPattern(); $desktop_url = $env_alias->getPattern();
break;
} }
} }
}else{ }else{
// current domain/alias is mobile // current domain/alias is desktop
$is_mobile_domain = false; $is_mobile_domain = false;
$desktop_url = $alias->getPattern(); $desktop_url = $alias->getPattern();
// find desktop pattern // 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) { foreach ($env_aliases as $env_alias_id => $env_alias) {
if($env_alias_id != $alias_id){ if($env_alias_id != $alias->id()){
// we found the desktop pattern // we found the desktop pattern
$mobile_url = $env_alias->getPattern(); $mobile_url = $env_alias->getPattern();
}
}
}
break; break;
} }
} }
break;
} }
} }
// $mobile =
$current_path = \Drupal::service('path.current')->getPath(); $current_path = \Drupal::service('path.current')->getPath();
$is_front = \Drupal::service('path.matcher')->isFrontPage(); $is_front = \Drupal::service('path.matcher')->isFrontPage();
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId(); $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();