materio_decoupled.module 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @file
  4. * Contains materio_decoupled.module.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. /**
  8. * Implements hook_help().
  9. */
  10. function materio_decoupled_help($route_name, RouteMatchInterface $route_match) {
  11. switch ($route_name) {
  12. // Main module help for the materio_decoupled module.
  13. case 'help.page.materio_decoupled':
  14. $output = '';
  15. $output .= '<h3>' . t('About') . '</h3>';
  16. $output .= '<p>' . t('helpers for progressive decoupling') . '</p>';
  17. return $output;
  18. default:
  19. }
  20. }
  21. /**
  22. * Implements hook_page_attachments().
  23. * @param array $attachments
  24. */
  25. function materio_decoupled_page_attachments(array &$attachments) {
  26. $current_path = \Drupal::service('path.current')->getPath();
  27. $route_name = \Drupal::routeMatch()->getRouteName();
  28. // $route_parameters = \Drupal::routeMatch()->getParameters()->all();
  29. // ksm($route_parameters);
  30. $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  31. $is_front = \Drupal::service('path.matcher')->isFrontPage();
  32. $entity_type = null;
  33. $entity_bundle = null;
  34. $entity_id = null;
  35. $entity_uuid = null;
  36. foreach (['node', 'taxonomy_term'] as $type) {
  37. $entity = \Drupal::routeMatch()->getParameter($type);
  38. if($entity){
  39. $entity_type = $type;
  40. $entity_bundle = $entity->bundle();
  41. $entity_id = $entity->id();
  42. $entity_uuid = $entity->uuid();
  43. break;
  44. }
  45. }
  46. // route_parameters:'".json_encode($route_parameters)."',\n
  47. $js_str = "var drupalDecoupled = {\n
  48. sys_path:'".$current_path."',\n
  49. route_name:'".$route_name."',\n
  50. is_front:".($is_front ? 'true':'false').",\n
  51. lang_code:'".$current_language."',\n
  52. entity_type:'".$entity_type."',\n
  53. entity_bundle:'".$entity_bundle."',\n
  54. entity_id:'".$entity_id."',\n
  55. entity_uuid:'".$entity_uuid."',\n
  56. };";
  57. $attachments['#attached']['html_head'][] = [
  58. [
  59. '#type' => 'html_tag',
  60. '#tag' => 'script',
  61. '#value' => $js_str,
  62. '#weight' => -999,
  63. '#group' => 'decoupled'
  64. ],
  65. // A key, to make it possible to recognize this HTML element when altering.
  66. 'decoupled',
  67. ];
  68. }