123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?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_hostname = preg_replace('/http:\/\/|https:\/\//i', '', $base_root);
- // dpm($current_hostname, '$current_hostname');
- $domain_storage = \Drupal::entityTypeManager()->getStorage('domain');
- $mobile_domain = $domain_storage->load('m_encyclopediedelaparole_org');
- // dpm($mobile_domain, 'mobile_domain');
- $desktop_domain = $domain_storage->load('encyclopediedelaparole_org');
- // dpm($desktop_domain, 'desktop_domain');
- $current_domain = $domain_storage->loadByHostname($current_hostname);
- // dpm($current_domain, 'current_domain');
- if($current_domain){
- // we are in a main domain
- $is_mobile_domain = ($current_domain->id() == $mobile_domain->id());
- $mobile_url = $mobile_domain->getHostname();
- $desktop_url = $desktop_domain->getHostname();
- }else{
- // we are not in a main domain, so we look for an alias
- $domain_alias_storage = \Drupal::entityTypeManager()->getStorage('domain_alias');
- $alias = $domain_alias_storage->loadByHostname($current_hostname);
- // dpm($alias, '$alias');
- $domain_id = $alias->getDomainId();
- // dpm($domain_id, 'domain_id');
- $current_domain = $domain_storage->load($domain_id);
- // dpm($current_domain, 'current_domain');
- $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
- // we assume that there is only two alias by env (mobile or desktop)
- 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();
- break;
- }
- }
- }else{
- // current domain/alias is desktop
- $is_mobile_domain = false;
- $desktop_url = $alias->getPattern();
- // find mobile pattern
- // we assume that there is only two alias by env (mobile or desktop)
- foreach ($env_aliases as $env_alias_id => $env_alias) {
- if($env_alias_id != $alias->id()){
- // we found the mobile pattern
- $mobile_url = $env_alias->getPattern();
- break;
- }
- }
- }
- }
- $current_path = \Drupal::service('path.current')->getPath();
- $is_front = \Drupal::service('path.matcher')->isFrontPage();
- $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
- // $config->set($domain_id . '.site_frontpage', $site_frontpage);
- // $mobile_home_path = "???";
- $mobile_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('m_encyclopediedelaparole_org.site_frontpage', FALSE);
- // $desktop_home_path = "???";
- $desktop_home_path = \Drupal::config('domain_site_settings.domainconfigsettings')->get('encyclopediedelaparole_org.site_frontpage', FALSE);
- // $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
- mobile_home_path:'".$mobile_home_path."',\n
- desktop_url:'".$desktop_url."',\n
- desktop_home_path:'".$desktop_home_path."',\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',
- ];
- }
|