Files
edlp_d8/web/modules/custom/edlp_mobile/edlp_mobile.module
T

168 lines
5.8 KiB
PHP

<?php
/**
* @file
* Contains edlp_mobile.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
// use Drupal\domain\DomainStorage;
// use Drupal\domain\DomainElementManager;
/**
* Implements hook_help().
*/
function edlp_mobile_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the edlp_mobile module.
case 'help.page.edlp_mobile':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Handle some mobile behaviours') . '</p>';
return $output;
default:
}
}
/**
* hook_entity_extra_field_info()
*/
// function edlp_mobile_entity_extra_field_info(){
// $extra = [];
// $extra['domain']['relations'] = [
// 'label' => t('Relations'),
// 'description' => 'Display enregistrement relations with other content types',
// 'weight' => 99,
// ];
// return $extra;
// }
// should be the right way
// function edlp_mobile_entity_field_storage_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
// if ($entity_type->id() == 'domain') {
// $definitions['device'] = \Drupal\Core\Field\BaseFieldDefinition::create('string')
// ->setName('Device')
// ->setLabel(t('Define a target device for this domain (mobile|desktop)'))
// ->setTargetEntityTypeId($entity_type->id());
// return $definitions;
// }
// }
/**
* Implements hook_page_attachments().
* the porpuse of this hooks is to collect registered domains informations to set it available to redirect.js file provided by edlptheme
* it does nothing by it's own
* @param array $attachments
*/
function edlp_mobile_page_attachments(array &$attachments) {
$is_mobile_domain = false;
global $base_root;
// dpm($base_root, '$base_root');
// get the current hostname (domain)
$current_hostname = preg_replace('/http:\/\/|https:\/\//i', '', $base_root);
// dpm($current_hostname, '$current_hostname');
// get domains from domain module
$domain_storage = \Drupal::entityTypeManager()->getStorage('domain');
$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');
$expo_domain = $domain_storage->load('expo_encyclopediedelaparole_org');
// dpm($expo_domain, 'expo_domain');
// get the current 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();
$expo_url = $expo_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
// get mobile pattern
foreach ($env_aliases as $env_alias_id => $env_alias) {
switch ($env_alias->getDomainId()) {
case 'm_encyclopediedelaparole_org':
$mobile_url = $env_alias->getPattern();
break;
case 'encyclopediedelaparole_org':
$desktop_url = $env_alias->getPattern();
break;
case 'expo_encyclopediedelaparole_org':
$expo_url = $env_alias->getPattern();
break;
}
}
// TODO: set target device as domain setting
if ($domain_id == 'm_encyclopediedelaparole_org'){
// current domain/alias is mobile
$is_mobile_domain = true;
}elseif($domain_id == 'expo_encyclopediedelaparole_org'){
// current domain/alias is expo
$is_mobile_domain = false;
}else{
// current domain/alias is desktop
$is_mobile_domain = false;
}
}
$current_path = \Drupal::service('path.current')->getPath();
$is_front = \Drupal::service('path.matcher')->isFrontPage();
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
// $config->set($domain_id . '.site_frontpage', $site_frontpage);
// $mobile_home_path = "???";
$mobile_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('m_encyclopediedelaparole_org.site_frontpage', FALSE);
// $desktop_home_path = "???";
$desktop_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('encyclopediedelaparole_org.site_frontpage', FALSE);
$expo_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('expo_encyclopediedelaparole_org.site_frontpage', FALSE);
// $redirect = false;
$js_str = "var edlp_mobile = {\n
current_url:'".$base_root."',\n
current_path:'".$current_path."',\n
is_front:".($is_front ? 'true':'false').",\n
lang_code:'".$current_language."',\n
is_mobile_domain:".($is_mobile_domain ? 'true':'false').",\n
mobile_url:'".$mobile_url."',\n
mobile_home_path:'".$mobile_home_path."',\n
desktop_url:'".$desktop_url."',\n
desktop_home_path:'".$desktop_home_path."',\n
expo_url:'".$expo_url."',\n
expo_home_path:'".$expo_home_path."',\n
};";
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => $js_str,
'#weight' => -1000,
'#group' => 'edlp'
],
// A key, to make it possible to recognize this HTML element when altering.
'edlp_mobile',
];
}