reha.theme 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. use Drupal\Core\Url;
  3. use Drupal\Core\Link;
  4. use Drupal\Core\Render\Markup;
  5. /**
  6. * @file
  7. * Functions to support theming in the reha theme.
  8. */
  9. /**
  10. * Implements hook_preprocess_HOOK() for html.html.twig.
  11. */
  12. function reha_preprocess_html(&$variables) {
  13. $node = \Drupal::routeMatch()->getParameter('node');
  14. if ($node){
  15. $variables['attributes']['class'][] = 'node-type-' . $node->bundle();
  16. $variables['attributes']['class'][] = 'node-id-' . $node->id();
  17. }
  18. }
  19. /**
  20. * Implements hook_preprocess_HOOK() for node.html.twig.
  21. */
  22. function reha_preprocess_node(&$variables) {
  23. $node = &$variables['node'];
  24. $variables['attributes']['class'][] = 'node-type-' . $node->gettype();
  25. if ($variables['node']->getType() == 'site') {
  26. $fields_to_exclude = [
  27. 'field_image', // Remplacez par le nom machine exact de votre champ image
  28. ];
  29. $filtered_content = [];
  30. $image_field_content = '';
  31. foreach ($variables['content'] as $field_name => $field_content) {
  32. if (!in_array($field_name, $fields_to_exclude)) {
  33. $filtered_content[$field_name] = $field_content;
  34. } else {
  35. $image_field_content = $field_content;
  36. }
  37. }
  38. $variables['filtered_content'] = $filtered_content;
  39. $variables['image_field_content'] = $image_field_content;
  40. }
  41. }
  42. /**
  43. * Implements hook_preprocess_HOOK() for page templates.
  44. */
  45. function reha_preprocess_page(&$variables) {
  46. // Vérifiez si c'est une page de nœud.
  47. if ($node = \Drupal::routeMatch()->getParameter('node')) {
  48. if ($node instanceof \Drupal\node\NodeInterface) {
  49. // Ajoutez une classe unique basée sur le nid (Node ID).
  50. $variables['attributes']['class'][] = 'page-node-' . $node->id();
  51. // Ajoutez un ID unique basé sur le nid.
  52. $variables['attributes']['id'] = 'page-node-' . $node->id();
  53. }
  54. }
  55. else {
  56. // Si ce n'est pas une page de nœud, vous pouvez ajouter un ID basé sur le nom de la route.
  57. $route_name = \Drupal::routeMatch()->getRouteName();
  58. $variables['attributes']['id'] = 'page-' . str_replace('.', '-', $route_name);
  59. }
  60. }
  61. // /**
  62. // * Implements hook_preprocess_HOOK() for node templates.
  63. // */
  64. // function reha_preprocess_node(&$variables) {
  65. // if ($variables['node']->getType() == 'site') {
  66. // $fields_to_exclude = [
  67. // 'field--type-image', // Remplacez par le nom machine exact de votre champ image
  68. // ];
  69. // $filtered_content = [];
  70. // $image_field_content = '';
  71. // foreach ($variables['content'] as $field_name => $field_content) {
  72. // if (!in_array($field_name, $fields_to_exclude)) {
  73. // $filtered_content[$field_name] = $field_content;
  74. // } else {
  75. // $image_field_content = $field_content;
  76. // }
  77. // }
  78. // $variables['filtered_content'] = $filtered_content;
  79. // $variables['image_field_content'] = $image_field_content;
  80. // }
  81. // }
  82. /**
  83. * Prepares variables for block templates.
  84. *
  85. * Default template: block.html.twig.
  86. *
  87. * Prepares the values passed to the theme_block function to be passed
  88. * into a pluggable template engine. Uses block properties to generate a
  89. * series of template file suggestions. If none are found, the default
  90. * block.html.twig is used.
  91. *
  92. * Most themes use their own copy of block.html.twig. The default is located
  93. * inside "core/modules/block/templates/block.html.twig". Look in there for the
  94. * full list of available variables.
  95. *
  96. * @param array $variables
  97. * An associative array containing:
  98. * - elements: An associative array containing the properties of the element.
  99. * Properties used: #block, #configuration, #children, #plugin_id.
  100. */
  101. //https://www.hashbangcode.com/article/drupal-9-programmatically-creating-and-using-urls-and-links
  102. function reha_preprocess_block(&$variables) {
  103. if ($variables['plugin_id'] === "user_login_block") {
  104. $url = new Url('user.register', [], ['query' => ['destination' => '/node/add/operation']]);
  105. $link = new Link('proposer une opération', $url);
  106. $variables['content']['reha'] = array(
  107. '#theme' => 'item_list',
  108. '#items' => [
  109. 'operations' => [
  110. "add_operation" => $link->toRenderable(),
  111. "description" => [
  112. "#markup" => Markup::create("<p>Créer un compte pour charger une opération</p>")
  113. ]
  114. ]
  115. ]
  116. );
  117. }
  118. if ($variables['plugin_id'] === "page_title_block") {
  119. if (\Drupal::routeMatch()->getRouteName() === 'node.add') {
  120. $node_type = \Drupal::routeMatch()->getParameter('node_type');
  121. if ($node_type->id() === 'operation') {
  122. $variables['content'] = [
  123. '#type' => 'page_title',
  124. '#title' => 'Proposer une opération'
  125. ];
  126. }
  127. }
  128. }
  129. // if ($variables['plugin_id'] == 'actus-block-1') {
  130. // $fields_to_exclude = [
  131. // 'field_image', // Remplacez par le nom machine exact du champ que vous souhaitez exclure
  132. // ];
  133. // $filtered_content = [];
  134. // $excluded_field_content = '';
  135. // foreach ($variables['content'] as $field_name => $field_content) {
  136. // if (!in_array($field_name, $fields_to_exclude)) {
  137. // $filtered_content[$field_name] = $field_content;
  138. // } else {
  139. // $excluded_field_content = $field_content;
  140. // }
  141. // }
  142. // $variables['filtered_content'] = $filtered_content;
  143. // $variables['excluded_field_content'] = $excluded_field_content;
  144. // }
  145. }