edlp_ajax.module 4.2 KB

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