mobile dev alpha, redirecting is working, started mobile display

This commit is contained in:
2018-04-26 17:43:50 +02:00
parent bf32cbaee8
commit 7601ad0b4c
16 changed files with 233 additions and 36 deletions
@@ -0,0 +1,7 @@
name: 'edlp_mobile'
type: module
description: 'Handle some mobile behaviours'
core: 8.x
package: 'Edlp'
dependencies:
- domain
@@ -0,0 +1,150 @@
<?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().
* @param array $attachments
*/
function edlp_mobile_page_attachments(array &$attachments) {
$is_mobile_domain = false;
global $base_root;
//dpm($base_root, '$base_root');
$current_domain = preg_replace('/http:\/\/|https:\/\//i', '', $base_root);
//dpm($current_domain, '$current_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) {
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();
}
}
}
break;
}
}
break;
}
}
// $mobile =
$current_path = \Drupal::service('path.current')->getPath();
$is_front = \Drupal::service('path.matcher')->isFrontPage();
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
// $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
desktop_url:'".$desktop_url."',\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',
];
}