edlp_mobile.module 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. $current_domain = $domain_storage->loadByHostname($current_hostname);
  61. // dpm($current_domain, 'current_domain');
  62. if($current_domain){
  63. // we are in a main domain
  64. $is_mobile_domain = ($current_domain->id() == $mobile_domain->id());
  65. $mobile_url = $mobile_domain->getHostname();
  66. $desktop_url = $desktop_domain->getHostname();
  67. }else{
  68. // we are not in a main domain, so we look for an alias
  69. $domain_alias_storage = \Drupal::entityTypeManager()->getStorage('domain_alias');
  70. $alias = $domain_alias_storage->loadByHostname($current_hostname);
  71. // dpm($alias, '$alias');
  72. $domain_id = $alias->getDomainId();
  73. // dpm($domain_id, 'domain_id');
  74. $current_domain = $domain_storage->load($domain_id);
  75. // dpm($current_domain, 'current_domain');
  76. $env = $alias->getEnvironment();
  77. $env_aliases = $domain_alias_storage->loadByEnvironment($env);
  78. // dpm($env_aliases, '$env_aliases');
  79. // find out mibile alias and desktop alias
  80. // this is dirty
  81. // TODO: set target device as domain setting
  82. if ($domain_id == 'm_encyclopediedelaparole_org'){
  83. // current domain/alias is mobile
  84. $is_mobile_domain = true;
  85. $mobile_url = $alias->getPattern();
  86. // find desktop pattern
  87. // we assume that there is only two alias by env (mobile or desktop)
  88. foreach ($env_aliases as $env_alias_id => $env_alias) {
  89. if($env_alias_id != $alias->id()){
  90. // we found the desktop pattern
  91. $desktop_url = $env_alias->getPattern();
  92. break;
  93. }
  94. }
  95. }else{
  96. // current domain/alias is desktop
  97. $is_mobile_domain = false;
  98. $desktop_url = $alias->getPattern();
  99. // find mobile pattern
  100. // we assume that there is only two alias by env (mobile or desktop)
  101. foreach ($env_aliases as $env_alias_id => $env_alias) {
  102. if($env_alias_id != $alias->id()){
  103. // we found the mobile pattern
  104. $mobile_url = $env_alias->getPattern();
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. $current_path = \Drupal::service('path.current')->getPath();
  111. $is_front = \Drupal::service('path.matcher')->isFrontPage();
  112. $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  113. // $config->set($domain_id . '.site_frontpage', $site_frontpage);
  114. // $mobile_home_path = "???";
  115. $mobile_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('m_encyclopediedelaparole_org.site_frontpage', FALSE);
  116. // $desktop_home_path = "???";
  117. $desktop_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('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. };";
  130. $attachments['#attached']['html_head'][] = [
  131. [
  132. '#type' => 'html_tag',
  133. '#tag' => 'script',
  134. '#value' => $js_str,
  135. '#weight' => -1000,
  136. '#group' => 'edlp'
  137. ],
  138. // A key, to make it possible to recognize this HTML element when altering.
  139. 'edlp_mobile',
  140. ];
  141. }