edlp_mobile.module 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * the porpuse of this hooks is to collect registered domains informations to set it available to redirect.js file provided by edlptheme
  48. * it does nothing by it's own
  49. * @param array $attachments
  50. */
  51. function edlp_mobile_page_attachments(array &$attachments) {
  52. $is_mobile_domain = false;
  53. global $base_root;
  54. // dpm($base_root, '$base_root');
  55. // get the current hostname (domain)
  56. $current_hostname = preg_replace('/http:\/\/|https:\/\//i', '', $base_root);
  57. // dpm($current_hostname, '$current_hostname');
  58. // get domains from domain module
  59. $domain_storage = \Drupal::entityTypeManager()->getStorage('domain');
  60. $mobile_domain = $domain_storage->load('m_encyclopediedelaparole_org');
  61. // dpm($mobile_domain, 'mobile_domain');
  62. $desktop_domain = $domain_storage->load('encyclopediedelaparole_org');
  63. // dpm($desktop_domain, 'desktop_domain');
  64. $expo_domain = $domain_storage->load('expo_encyclopediedelaparole_org');
  65. // dpm($expo_domain, 'expo_domain');
  66. // get the current domain
  67. $current_domain = $domain_storage->loadByHostname($current_hostname);
  68. // dpm($current_domain, 'current_domain');
  69. if($current_domain){
  70. // we are in a main domain
  71. $is_mobile_domain = ($current_domain->id() == $mobile_domain->id());
  72. $mobile_url = $mobile_domain->getHostname();
  73. $desktop_url = $desktop_domain->getHostname();
  74. $expo_url = $expo_domain->getHostname();
  75. }else{
  76. // we are not in a main domain, so we look for an alias
  77. $domain_alias_storage = \Drupal::entityTypeManager()->getStorage('domain_alias');
  78. $alias = $domain_alias_storage->loadByHostname($current_hostname);
  79. // dpm($alias, '$alias');
  80. $domain_id = $alias->getDomainId();
  81. // dpm($domain_id, 'domain_id');
  82. $current_domain = $domain_storage->load($domain_id);
  83. // dpm($current_domain, 'current_domain');
  84. $env = $alias->getEnvironment();
  85. $env_aliases = $domain_alias_storage->loadByEnvironment($env);
  86. // dpm($env_aliases, '$env_aliases');
  87. // find out mibile alias and desktop alias
  88. // get mobile pattern
  89. foreach ($env_aliases as $env_alias_id => $env_alias) {
  90. switch ($env_alias->getDomainId()) {
  91. case 'm_encyclopediedelaparole_org':
  92. $mobile_url = $env_alias->getPattern();
  93. break;
  94. case 'encyclopediedelaparole_org':
  95. $desktop_url = $env_alias->getPattern();
  96. break;
  97. case 'expo_encyclopediedelaparole_org':
  98. $expo_url = $env_alias->getPattern();
  99. break;
  100. }
  101. }
  102. // TODO: set target device as domain setting
  103. if ($domain_id == 'm_encyclopediedelaparole_org'){
  104. // current domain/alias is mobile
  105. $is_mobile_domain = true;
  106. }elseif($domain_id == 'expo_encyclopediedelaparole_org'){
  107. // current domain/alias is expo
  108. $is_mobile_domain = false;
  109. }else{
  110. // current domain/alias is desktop
  111. $is_mobile_domain = false;
  112. }
  113. }
  114. $current_path = \Drupal::service('path.current')->getPath();
  115. $is_front = \Drupal::service('path.matcher')->isFrontPage();
  116. $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  117. // $config->set($domain_id . '.site_frontpage', $site_frontpage);
  118. // $mobile_home_path = "???";
  119. $mobile_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('m_encyclopediedelaparole_org.site_frontpage', FALSE);
  120. // $desktop_home_path = "???";
  121. $desktop_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('encyclopediedelaparole_org.site_frontpage', FALSE);
  122. $expo_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('expo_encyclopediedelaparole_org.site_frontpage', FALSE);
  123. // $redirect = false;
  124. $js_str = "var edlp_mobile = {\n
  125. current_url:'".$base_root."',\n
  126. current_path:'".$current_path."',\n
  127. is_front:".($is_front ? 'true':'false').",\n
  128. lang_code:'".$current_language."',\n
  129. is_mobile_domain:".($is_mobile_domain ? 'true':'false').",\n
  130. mobile_url:'".$mobile_url."',\n
  131. mobile_home_path:'".$mobile_home_path."',\n
  132. desktop_url:'".$desktop_url."',\n
  133. desktop_home_path:'".$desktop_home_path."',\n
  134. expo_url:'".$expo_url."',\n
  135. expo_home_path:'".$expo_home_path."',\n
  136. };";
  137. $attachments['#attached']['html_head'][] = [
  138. [
  139. '#type' => 'html_tag',
  140. '#tag' => 'script',
  141. '#value' => $js_str,
  142. '#weight' => -1000,
  143. '#group' => 'edlp'
  144. ],
  145. // A key, to make it possible to recognize this HTML element when altering.
  146. 'edlp_mobile',
  147. ];
  148. }