123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?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',
- ];
- }
|