edlp_ajax.module 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // do not redirect if not node, term, or custom routing
  21. // FIXME: check with routes instead of path as path can change !!!
  22. if(preg_match('/^\/?node\/\d+/', $current_path)
  23. || preg_match('/^\/?taxonomy\/term\/\d+/', $current_path)
  24. || preg_match('/^\/?productions/', $current_path)
  25. || preg_match('/^\/?agenda/', $current_path)
  26. || preg_match('/^\/?studio/', $current_path)
  27. || preg_match('/^\/?search/', $current_path)){
  28. $redirect = true;
  29. }
  30. // $redirect = false;
  31. $js_str = "var edlp = {\n
  32. sys_path:'".$current_path."',\n
  33. is_front:".($is_front ? 'true':'false').",\n
  34. redirect:".($redirect ? 'true':'false').",\n
  35. lang_code:'".$current_language."',\n
  36. };";
  37. $attachments['#attached']['html_head'][] = [
  38. [
  39. '#type' => 'html_tag',
  40. '#tag' => 'script',
  41. '#value' => $js_str,
  42. '#weight' => -999,
  43. '#group' => 'edlp'
  44. ],
  45. // A key, to make it possible to recognize this HTML element when altering.
  46. 'edlp',
  47. ];
  48. }
  49. /**
  50. * Implements hook_theme().
  51. */
  52. function edlp_ajax_theme($existing, $type, $theme, $path) {
  53. // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  54. return array(
  55. 'edlp_ajax' => array(
  56. 'file' => 'includes/edlp_ajax.inc',
  57. 'variables' => array(
  58. 'entity_type' => 'node',
  59. 'entity' => NULL,
  60. 'view_mode' => 'default'
  61. ),
  62. ),
  63. );
  64. }
  65. /**
  66. * Implements hook_theme_suggestions_HOOK().
  67. */
  68. function edlp_ajax_theme_suggestions_edlp_ajax(array $vars) {
  69. // dpm($vars);
  70. $suggestions = [];
  71. // $node = $variables['elements']['#node'];
  72. $sanitized_view_mode = strtr($vars['view_mode'], '.', '_');
  73. //
  74. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'];
  75. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $sanitized_view_mode;
  76. // $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
  77. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id();
  78. $suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id() . '__' . $sanitized_view_mode;
  79. return $suggestions;
  80. }