edlp_mobile.module 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @file
  4. * Contains edlp_mobile.module.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. // use Drupal\domain\DomainStorage;
  8. // use Drupal\domain\DomainElementManager;
  9. /**
  10. * Implements hook_help().
  11. */
  12. function edlp_mobile_help($route_name, RouteMatchInterface $route_match) {
  13. switch ($route_name) {
  14. // Main module help for the edlp_mobile module.
  15. case 'help.page.edlp_mobile':
  16. $output = '';
  17. $output .= '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('Handle some mobile behaviours') . '</p>';
  19. return $output;
  20. default:
  21. }
  22. }
  23. /**
  24. * hook_entity_extra_field_info()
  25. */
  26. // function edlp_mobile_entity_extra_field_info(){
  27. // $extra = [];
  28. // $extra['domain']['relations'] = [
  29. // 'label' => t('Relations'),
  30. // 'description' => 'Display enregistrement relations with other content types',
  31. // 'weight' => 99,
  32. // ];
  33. // return $extra;
  34. // }
  35. // should be the right way
  36. // function edlp_mobile_entity_field_storage_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  37. // if ($entity_type->id() == 'domain') {
  38. // $definitions['device'] = \Drupal\Core\Field\BaseFieldDefinition::create('string')
  39. // ->setName('Device')
  40. // ->setLabel(t('Define a target device for this domain (mobile|desktop)'))
  41. // ->setTargetEntityTypeId($entity_type->id());
  42. // return $definitions;
  43. // }
  44. // }
  45. /**
  46. * Implements hook_page_attachments().
  47. * @param array $attachments
  48. */
  49. function edlp_mobile_page_attachments(array &$attachments) {
  50. $is_mobile_domain = false;
  51. global $base_root;
  52. // dpm($base_root, '$base_root');
  53. $current_hostname = preg_replace('/http:\/\/|https:\/\//i', '', $base_root);
  54. // dpm($current_hostname, '$current_hostname');
  55. $domain_storage = \Drupal::entityTypeManager()->getStorage('domain');
  56. $mobile_domain = $domain_storage->load('m_encyclopediedelaparole_org');
  57. // dpm($mobile_domain, 'mobile_domain');
  58. $desktop_domain = $domain_storage->load('encyclopediedelaparole_org');
  59. // dpm($desktop_domain, 'desktop_domain');
  60. $expo_domain = $domain_storage->load('expo_encyclopediedelaparole_org');
  61. // dpm($expo_domain, 'expo_domain');
  62. $current_domain = $domain_storage->loadByHostname($current_hostname);
  63. // dpm($current_domain, 'current_domain');
  64. if($current_domain){
  65. // we are in a main domain
  66. $is_mobile_domain = ($current_domain->id() == $mobile_domain->id());
  67. $mobile_url = $mobile_domain->getHostname();
  68. $desktop_url = $desktop_domain->getHostname();
  69. $expo_url = $expo_domain->getHostname();
  70. }else{
  71. // we are not in a main domain, so we look for an alias
  72. $domain_alias_storage = \Drupal::entityTypeManager()->getStorage('domain_alias');
  73. $alias = $domain_alias_storage->loadByHostname($current_hostname);
  74. // dpm($alias, '$alias');
  75. $domain_id = $alias->getDomainId();
  76. // dpm($domain_id, 'domain_id');
  77. $current_domain = $domain_storage->load($domain_id);
  78. // dpm($current_domain, 'current_domain');
  79. $env = $alias->getEnvironment();
  80. $env_aliases = $domain_alias_storage->loadByEnvironment($env);
  81. // dpm($env_aliases, '$env_aliases');
  82. // find out mibile alias and desktop alias
  83. // get mobile pattern
  84. foreach ($env_aliases as $env_alias_id => $env_alias) {
  85. switch ($env_alias->getDomainId()) {
  86. case 'm_encyclopediedelaparole_org':
  87. $mobile_url = $env_alias->getPattern();
  88. break;
  89. case 'encyclopediedelaparole_org':
  90. $desktop_url = $env_alias->getPattern();
  91. break;
  92. case 'expo_encyclopediedelaparole_org':
  93. $expo_url = $env_alias->getPattern();
  94. break;
  95. }
  96. }
  97. // TODO: set target device as domain setting
  98. if ($domain_id == 'm_encyclopediedelaparole_org'){
  99. // current domain/alias is mobile
  100. $is_mobile_domain = true;
  101. }elseif($domain_id == 'expo_encyclopediedelaparole_org'){
  102. // current domain/alias is expo
  103. $is_mobile_domain = false;
  104. }else{
  105. // current domain/alias is desktop
  106. $is_mobile_domain = false;
  107. }
  108. }
  109. $current_path = \Drupal::service('path.current')->getPath();
  110. $is_front = \Drupal::service('path.matcher')->isFrontPage();
  111. $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  112. // $config->set($domain_id . '.site_frontpage', $site_frontpage);
  113. // $mobile_home_path = "???";
  114. $mobile_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('m_encyclopediedelaparole_org.site_frontpage', FALSE);
  115. // $desktop_home_path = "???";
  116. $desktop_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('encyclopediedelaparole_org.site_frontpage', FALSE);
  117. $expo_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('expo_encyclopediedelaparole_org.site_frontpage', FALSE);
  118. // $redirect = false;
  119. $js_str = "var edlp_mobile = {\n
  120. current_url:'".$base_root."',\n
  121. current_path:'".$current_path."',\n
  122. is_front:".($is_front ? 'true':'false').",\n
  123. lang_code:'".$current_language."',\n
  124. is_mobile_domain:".($is_mobile_domain ? 'true':'false').",\n
  125. mobile_url:'".$mobile_url."',\n
  126. mobile_home_path:'".$mobile_home_path."',\n
  127. desktop_url:'".$desktop_url."',\n
  128. desktop_home_path:'".$desktop_home_path."',\n
  129. expo_url:'".$expo_url."',\n
  130. expo_home_path:'".$expo_home_path."',\n
  131. };";
  132. $attachments['#attached']['html_head'][] = [
  133. [
  134. '#type' => 'html_tag',
  135. '#tag' => 'script',
  136. '#value' => $js_str,
  137. '#weight' => -1000,
  138. '#group' => 'edlp'
  139. ],
  140. // A key, to make it possible to recognize this HTML element when altering.
  141. 'edlp_mobile',
  142. ];
  143. }