edlp_ajax.module 2.8 KB

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