materio_decoupled.module 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. $current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  28. $is_front = \Drupal::service('path.matcher')->isFrontPage();
  29. $entity_type = null;
  30. $entity_bundle = null;
  31. $entity_id = null;
  32. $entity_uuid = null;
  33. foreach (['node', 'taxonomy_term'] as $type) {
  34. $entity = \Drupal::routeMatch()->getParameter($type);
  35. if($entity){
  36. $entity_type = $type;
  37. $entity_bundle = $entity->bundle();
  38. $entity_id = $entity->id();
  39. $entity_uuid = $entity->uuid();
  40. break;
  41. }
  42. }
  43. $js_str = "var drupalDecoupled = {\n
  44. sys_path:'".$current_path."',\n
  45. is_front:".($is_front ? 'true':'false').",\n
  46. lang_code:'".$current_language."',\n
  47. entity_type:'".$entity_type."',\n
  48. entity_bundle:'".$entity_bundle."',\n
  49. entity_id:'".$entity_id."',\n
  50. entity_uuid:'".$entity_uuid."',\n
  51. };";
  52. $attachments['#attached']['html_head'][] = [
  53. [
  54. '#type' => 'html_tag',
  55. '#tag' => 'script',
  56. '#value' => $js_str,
  57. '#weight' => -999,
  58. '#group' => 'decoupled'
  59. ],
  60. // A key, to make it possible to recognize this HTML element when altering.
  61. 'decoupled',
  62. ];
  63. }