edlp_mobile.module 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_domain = preg_replace('/http:\/\/|https:\/\//i', '', $base_root);
  54. //dpm($current_domain, '$current_domain');
  55. $domain_storage = \Drupal::entityTypeManager()->getStorage('domain');
  56. $domains = $domain_storage->loadMultiple();
  57. //dpm($domains);
  58. $domain_alias_storage = \Drupal::entityTypeManager()->getStorage('domain_alias');
  59. //dpm($domain_alias_storage);
  60. foreach ($domains as $domain_id => $domain) {
  61. if($domain->isActive()){
  62. //dpm($domain);
  63. $alias_ids = $domain_alias_storage->getQuery()->condition('domain_id', $domain_id)->execute();
  64. //dpm($alias_ids, 'alias_ids');
  65. $aliases = $domain_alias_storage->loadMultiple($alias_ids);
  66. // dpm($aliases, 'aliases');
  67. foreach ($aliases as $alias_id => $alias) {
  68. if($alias->getPattern() == $current_domain){
  69. // we found the current domain and current alias
  70. dpm($domain, 'domain');
  71. dpm($alias, 'alias');
  72. $env = $alias->getEnvironment();
  73. $env_aliases = $domain_alias_storage->loadByEnvironment($env);
  74. dpm($env_aliases, '$env_aliases');
  75. // find out mibile alias and desktop alias
  76. // this is dirty
  77. // TODO: set target device as domain setting
  78. if ($domain->id() == 'm_encyclopediedelaparole_org'){
  79. // current domain/alias is mobile
  80. $is_mobile_domain = true;
  81. $mobile_url = $alias->getPattern();
  82. // find desktop pattern
  83. foreach ($env_aliases as $env_alias_id => $env_alias) {
  84. if($env_alias_id != $alias_id){
  85. // we found the desktop pattern
  86. $desktop_url = $env_alias->getPattern();
  87. }
  88. }
  89. }else{
  90. // current domain/alias is mobile
  91. $is_mobile_domain = false;
  92. $desktop_url = $alias->getPattern();
  93. // find desktop pattern
  94. foreach ($env_aliases as $env_alias_id => $env_alias) {
  95. if($env_alias_id != $alias_id){
  96. // we found the desktop pattern
  97. $mobile_url = $env_alias->getPattern();
  98. }
  99. }
  100. }
  101. break;
  102. }
  103. }
  104. break;
  105. }
  106. }
  107. // $mobile =
  108. $current_path = \Drupal::service('path.current')->getPath();
  109. $is_front = \Drupal::service('path.matcher')->isFrontPage();
  110. $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  111. // $redirect = false;
  112. $js_str = "var edlp_mobile = {\n
  113. current_url:'".$base_root."',\n
  114. current_path:'".$current_path."',\n
  115. is_front:".($is_front ? 'true':'false').",\n
  116. lang_code:'".$current_language."',\n
  117. is_mobile_domain:".($is_mobile_domain ? 'true':'false').",\n
  118. mobile_url:'".$mobile_url."',\n
  119. desktop_url:'".$desktop_url."',\n
  120. };";
  121. $attachments['#attached']['html_head'][] = [
  122. [
  123. '#type' => 'html_tag',
  124. '#tag' => 'script',
  125. '#value' => $js_str,
  126. '#weight' => -1000,
  127. '#group' => 'edlp'
  128. ],
  129. // A key, to make it possible to recognize this HTML element when altering.
  130. 'edlp_mobile',
  131. ];
  132. }