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);
$domain_alias_storage = \Drupal::entityTypeManager()->getStorage('domain_alias');
//dpm($domain_alias_storage);
foreach ($domains as $domain_id => $domain) { $mobile_domain = $domain_storage->load('m_encyclopediedelaparole_org');
if($domain->isActive()){ // dpm($mobile_domain, 'mobile_domain');
//dpm($domain);
$alias_ids = $domain_alias_storage->getQuery()->condition('domain_id', $domain_id)->execute(); $desktop_domain = $domain_storage->load('encyclopediedelaparole_org');
//dpm($alias_ids, 'alias_ids'); // dpm($desktop_domain, 'desktop_domain');
$aliases = $domain_alias_storage->loadMultiple($alias_ids); $current_domain = $domain_storage->loadByHostname($current_hostname);
// dpm($aliases, 'aliases'); // dpm($current_domain, 'current_domain');
foreach ($aliases as $alias_id => $alias) { if($current_domain){
if($alias->getPattern() == $current_domain){ // we are in a main domain
// we found the current domain and current alias $is_mobile_domain = ($current_domain->id() == $mobile_domain->id());
// dpm($domain, 'domain'); $mobile_url = $mobile_domain->getHostname();
// dpm($alias, 'alias'); $desktop_url = $desktop_domain->getHostname();
$env = $alias->getEnvironment(); }else{
$env_aliases = $domain_alias_storage->loadByEnvironment($env); // we are not in a main domain, so we look for an alias
// dpm($env_aliases, '$env_aliases'); $domain_alias_storage = \Drupal::entityTypeManager()->getStorage('domain_alias');
// find out mibile alias and desktop alias $alias = $domain_alias_storage->loadByHostname($current_hostname);
// this is dirty // dpm($alias, '$alias');
// TODO: set target device as domain setting $domain_id = $alias->getDomainId();
if ($domain->id() == 'm_encyclopediedelaparole_org'){ // dpm($domain_id, 'domain_id');
// current domain/alias is mobile $current_domain = $domain_storage->load($domain_id);
$is_mobile_domain = true; // dpm($current_domain, 'current_domain');
$mobile_url = $alias->getPattern();
// find desktop pattern $env = $alias->getEnvironment();
foreach ($env_aliases as $env_alias_id => $env_alias) { $env_aliases = $domain_alias_storage->loadByEnvironment($env);
if($env_alias_id != $alias_id){ // dpm($env_aliases, '$env_aliases');
// we found the desktop pattern // find out mibile alias and desktop alias
$desktop_url = $env_alias->getPattern(); // this is dirty
} // TODO: set target device as domain setting
} if ($domain_id == 'm_encyclopediedelaparole_org'){
}else{ // current domain/alias is mobile
// current domain/alias is mobile $is_mobile_domain = true;
$is_mobile_domain = false; $mobile_url = $alias->getPattern();
$desktop_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
$mobile_url = $env_alias->getPattern(); $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;
} }
} }
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();