edlp_ajax.module 4.1 KB

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