edlp_ajax.module 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. # @Author: Bachir Soussi Chiadmi <bach>
  3. # @Email: bachir@figureslibres.io
  4. # @Filename: edlp_ajax.module
  5. # @License: GPL-V3
  6. use Drupal\Core\Url;
  7. /**
  8. * Implements hook_page_attachments().
  9. * @param array $attachments
  10. */
  11. function edlp_ajax_page_attachments(array &$attachments) {
  12. // provied info about available urls needed for ajax purpose
  13. $url = Url::fromRoute('edlp_ajax.entityjson');
  14. $attachments['#attached']['drupalSettings']['edlp_ajax']['entityjson_path'] = $url->getInternalPath();
  15. $url = Url::fromRoute('edlp_ajax.blocksjson');
  16. $attachments['#attached']['drupalSettings']['edlp_ajax']['blocksjson_path'] = $url->getInternalPath();
  17. // provied infos about current entity (node or taxonomy term) and audio files
  18. $current_path = \Drupal::service('path.current')->getPath();
  19. $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  20. $is_front = \Drupal::service('path.matcher')->isFrontPage();
  21. $entity_type = null;
  22. $entity_bundle = null;
  23. $entity_id = null;
  24. $audio_url = null;
  25. foreach (['node', 'taxonomy_term'] as $type) {
  26. $entity = \Drupal::routeMatch()->getParameter($type);
  27. if($entity){
  28. $entity_type = $type;
  29. $entity_bundle = $entity->bundle();
  30. $entity_id = $entity->id();
  31. if($entity_bundle == 'enregistrement'){
  32. $field_son_values = $entity->get('field_son')->getValue();
  33. $audio_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : null;
  34. if($audio_fid){
  35. $audio_file = \Drupal\file\Entity\File::load($audio_fid);
  36. $son_uri = $audio_file->getFileUri();
  37. $audio_url = file_create_url($son_uri);
  38. }
  39. }
  40. break;
  41. }
  42. }
  43. // do not redirect if not node, term, or custom routing
  44. // FIXME: check with routes instead of path as path can change !!!
  45. $redirect = false;
  46. if(preg_match('/^\/?node\/\d+/', $current_path)
  47. || preg_match('/^\/?taxonomy\/term\/\d+/', $current_path)
  48. || preg_match('/^\/?productions/', $current_path)
  49. || preg_match('/^\/?agenda/', $current_path)
  50. || preg_match('/^\/?studio/', $current_path)
  51. || preg_match('/^\/?search/', $current_path)
  52. || preg_match('/^\/?docsindex/', $current_path)
  53. || preg_match('/^\/?lastdocs/', $current_path)
  54. || preg_match('/^\/?articles/', $current_path)){
  55. $redirect = true;
  56. }
  57. $site_name = \Drupal::config('system.site')->get('name');
  58. $js_str = "var edlp = {\n
  59. sys_path:'".$current_path."',\n
  60. is_front:".($is_front ? 'true':'false').",\n
  61. redirect:".($redirect ? 'true':'false').",\n
  62. lang_code:'".$current_language."',\n
  63. entity_type:'".$entity_type."',\n
  64. entity_bundle:'".$entity_bundle."',\n
  65. entity_id:'".$entity_id."',\n
  66. audio_url:'".$audio_url."',\n
  67. site_name:'".$site_name."',\n
  68. };";
  69. $attachments['#attached']['html_head'][] = [
  70. [
  71. '#type' => 'html_tag',
  72. '#tag' => 'script',
  73. '#value' => $js_str,
  74. '#weight' => -999,
  75. '#group' => 'edlp'
  76. ],
  77. // A key, to make it possible to recognize this HTML element when altering.
  78. 'edlp',
  79. ];
  80. }
  81. /**
  82. * Implements hook_theme().
  83. */
  84. function edlp_ajax_theme($existing, $type, $theme, $path) {
  85. // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  86. return array(
  87. 'edlp_ajax' => array(
  88. 'file' => 'includes/edlp_ajax.inc',
  89. 'variables' => array(
  90. 'entity_type' => null,
  91. 'bundle' => null,
  92. 'entity' => null,
  93. 'view_mode' => 'default',
  94. 'aside' => array(),
  95. ),
  96. ),
  97. );
  98. }
  99. /**
  100. * Implements hook_theme_suggestions_HOOK().
  101. */
  102. function edlp_ajax_theme_suggestions_edlp_ajax(array $vars) {
  103. // dpm($vars);
  104. $suggestions = [];
  105. // $node = $variables['elements']['#node'];
  106. $sanitized_view_mode = strtr($vars['view_mode'], '.', '_');
  107. // $entity = $vars['entity'];
  108. //
  109. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'];
  110. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $sanitized_view_mode;
  111. // $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
  112. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id();
  113. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id() . '__' . $sanitized_view_mode;
  114. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['bundle'];
  115. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['bundle'] . '__' . $sanitized_view_mode;
  116. return $suggestions;
  117. }