edlp_ajax.module 2.7 KB

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