edlp_ajax.module 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $redirect = true;
  51. }
  52. // $redirect = false;
  53. $js_str = "var edlp = {\n
  54. sys_path:'".$current_path."',\n
  55. is_front:".($is_front ? 'true':'false').",\n
  56. redirect:".($redirect ? 'true':'false').",\n
  57. lang_code:'".$current_language."',\n
  58. entity_type:'".$entity_type."',\n
  59. entity_bundle:'".$entity_bundle."',\n
  60. entity_id:'".$entity_id."',\n
  61. audio_url:'".$audio_url."',\n
  62. };";
  63. $attachments['#attached']['html_head'][] = [
  64. [
  65. '#type' => 'html_tag',
  66. '#tag' => 'script',
  67. '#value' => $js_str,
  68. '#weight' => -999,
  69. '#group' => 'edlp'
  70. ],
  71. // A key, to make it possible to recognize this HTML element when altering.
  72. 'edlp',
  73. ];
  74. }
  75. /**
  76. * Implements hook_theme().
  77. */
  78. function edlp_ajax_theme($existing, $type, $theme, $path) {
  79. // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  80. return array(
  81. 'edlp_ajax' => array(
  82. 'file' => 'includes/edlp_ajax.inc',
  83. 'variables' => array(
  84. 'entity_type' => null,
  85. 'bundle' => null,
  86. 'entity' => null,
  87. 'view_mode' => 'default',
  88. 'aside' => array(),
  89. ),
  90. ),
  91. );
  92. }
  93. /**
  94. * Implements hook_theme_suggestions_HOOK().
  95. */
  96. function edlp_ajax_theme_suggestions_edlp_ajax(array $vars) {
  97. // dpm($vars);
  98. $suggestions = [];
  99. // $node = $variables['elements']['#node'];
  100. $sanitized_view_mode = strtr($vars['view_mode'], '.', '_');
  101. // $entity = $vars['entity'];
  102. //
  103. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'];
  104. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $sanitized_view_mode;
  105. // $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
  106. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id();
  107. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id() . '__' . $sanitized_view_mode;
  108. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['bundle'];
  109. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['bundle'] . '__' . $sanitized_view_mode;
  110. return $suggestions;
  111. }