123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- # @Author: Bachir Soussi Chiadmi <bach>
- # @Email: bachir@figureslibres.io
- # @Filename: edlp_ajax.module
- # @License: GPL-V3
- use Drupal\Core\Url;
- /**
- * Implements hook_page_attachments().
- * @param array $attachments
- */
- function edlp_ajax_page_attachments(array &$attachments) {
- $url = Url::fromRoute('edlp_ajax.entityjson');
- $attachments['#attached']['drupalSettings']['edlp_ajax']['entityjson_path'] = $url->getInternalPath();
- $url = Url::fromRoute('edlp_ajax.blocksjson');
- $attachments['#attached']['drupalSettings']['edlp_ajax']['blocksjson_path'] = $url->getInternalPath();
- $redirect = false;
- $current_path = \Drupal::service('path.current')->getPath();
- $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
- $is_front = \Drupal::service('path.matcher')->isFrontPage();
- $entity_type = null;
- $entity_bundle = null;
- $entity_id = null;
- $audio_url = null;
- foreach (['node', 'taxonomy_term'] as $type) {
- $entity = \Drupal::routeMatch()->getParameter($type);
- if($entity){
- $entity_type = $type;
- $entity_bundle = $entity->bundle();
- $entity_id = $entity->id();
- if($entity_bundle == 'enregistrement'){
- $field_son_values = $entity->get('field_son')->getValue();
- $audio_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : null;
- if($audio_fid){
- $audio_file = \Drupal\file\Entity\File::load($audio_fid);
- $son_uri = $audio_file->getFileUri();
- $audio_url = file_create_url($son_uri);
- }
- }
- break;
- }
- }
- // do not redirect if not node, term, or custom routing
- // FIXME: check with routes instead of path as path can change !!!
- if(preg_match('/^\/?node\/\d+/', $current_path)
- || preg_match('/^\/?taxonomy\/term\/\d+/', $current_path)
- || preg_match('/^\/?productions/', $current_path)
- || preg_match('/^\/?agenda/', $current_path)
- || preg_match('/^\/?studio/', $current_path)
- || preg_match('/^\/?search/', $current_path)
- || preg_match('/^\/?docsindex/', $current_path)
- || preg_match('/^\/?lastdocs/', $current_path)
- || preg_match('/^\/?articles/', $current_path)){
- $redirect = true;
- }
- $site_name = \Drupal::config('system.site')->get('name');
- $js_str = "var edlp = {\n
- sys_path:'".$current_path."',\n
- is_front:".($is_front ? 'true':'false').",\n
- redirect:".($redirect ? 'true':'false').",\n
- lang_code:'".$current_language."',\n
- entity_type:'".$entity_type."',\n
- entity_bundle:'".$entity_bundle."',\n
- entity_id:'".$entity_id."',\n
- audio_url:'".$audio_url."',\n
- site_name:'".$site_name."',\n
- };";
- $attachments['#attached']['html_head'][] = [
- [
- '#type' => 'html_tag',
- '#tag' => 'script',
- '#value' => $js_str,
- '#weight' => -999,
- '#group' => 'edlp'
- ],
- // A key, to make it possible to recognize this HTML element when altering.
- 'edlp',
- ];
- }
- /**
- * Implements hook_theme().
- */
- function edlp_ajax_theme($existing, $type, $theme, $path) {
- // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
- return array(
- 'edlp_ajax' => array(
- 'file' => 'includes/edlp_ajax.inc',
- 'variables' => array(
- 'entity_type' => null,
- 'bundle' => null,
- 'entity' => null,
- 'view_mode' => 'default',
- 'aside' => array(),
- ),
- ),
- );
- }
- /**
- * Implements hook_theme_suggestions_HOOK().
- */
- function edlp_ajax_theme_suggestions_edlp_ajax(array $vars) {
- // dpm($vars);
- $suggestions = [];
- // $node = $variables['elements']['#node'];
- $sanitized_view_mode = strtr($vars['view_mode'], '.', '_');
- // $entity = $vars['entity'];
- //
- $suggestions[] = 'edlp_ajax__' . $vars['entity_type'];
- $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $sanitized_view_mode;
- // $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
- $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id();
- $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id() . '__' . $sanitized_view_mode;
- $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['bundle'];
- $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['bundle'] . '__' . $sanitized_view_mode;
- return $suggestions;
- }
|