edlp_ajax.module 4.3 KB

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