reha.theme 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. use Drupal\Core\Url;
  3. use Drupal\Core\Link;
  4. use Drupal\Core\Render\Markup;
  5. use Drupal\Component\Utility\Html;
  6. /**
  7. * @file
  8. * Functions to support theming in the reha theme.
  9. */
  10. /**
  11. * Implements hook_preprocess_HOOK() for html.html.twig.
  12. */
  13. function reha_preprocess_html(&$variables) {
  14. $node = \Drupal::routeMatch()->getParameter('node');
  15. if ($node){
  16. $variables['attributes']['class'][] = 'node-type-' . $node->bundle();
  17. $variables['attributes']['class'][] = 'node-id-' . $node->id();
  18. }
  19. }
  20. /**
  21. * Implements hook_preprocess_HOOK() for node.html.twig.
  22. */
  23. function reha_preprocess_node(&$variables) {
  24. $node = &$variables['node'];
  25. $variables['attributes']['class'][] = 'node-type-' . $node->gettype();
  26. if ($node->getType() == 'site' || $node->getType() == 'ressource' || $node->getType() == 'actualite') {
  27. $fields_to_exclude = [
  28. 'field_image', // Remplacez par le nom machine exact de votre champ image
  29. ];
  30. $filtered_content = [];
  31. $image_field_content = '';
  32. $body_field_content = '';
  33. $files_field_content = '';
  34. $liens_field_content = '';
  35. foreach ($variables['content'] as $field_name => $field_content) {
  36. if ($field_name == 'body') {
  37. $body_field_content = $field_content;
  38. } elseif ($field_name == 'field_fichiers') {
  39. $files_field_content = $field_content;
  40. } elseif ($field_name == 'field_liens') {
  41. $liens_field_content = $field_content;
  42. } elseif (!in_array($field_name, $fields_to_exclude)) {
  43. $filtered_content[$field_name] = $field_content;
  44. } else {
  45. $image_field_content = $field_content;
  46. }
  47. }
  48. $variables['filtered_content'] = $filtered_content;
  49. $variables['image_field_content'] = $image_field_content;
  50. $variables['body_field_content'] = $body_field_content;
  51. $variables['files_field_content'] = $files_field_content;
  52. $variables['liens_field_content'] = $liens_field_content;
  53. }
  54. // if ($node->getType() == 'site') {
  55. // $blocks_to_exclude = [
  56. // 'block-entity-fieldnodefield-image',
  57. // ];
  58. // $filtered_block_content = [];
  59. // $image_block_content = '';
  60. // foreach ($variables['content'] as $block_name => $field_content) {
  61. // if (!in_array($block_name, $blocks_to_exclude)) {
  62. // $filtered_block_content[$block_name] = $field_content;
  63. // } else {
  64. // $image_block_content = $field_content;
  65. // }
  66. // }
  67. // // Set variables for the template
  68. // $variables['filtered_block_content'] = $filtered_block_content;
  69. // $variables['image_block_content'] = $image_block_content;
  70. // }
  71. }
  72. /**
  73. * Implements hook_preprocess_HOOK() for page templates.
  74. */
  75. // function reha_preprocess_page(&$variables) {
  76. // // Vérifiez si c'est une page de nœud.
  77. // if ($node = \Drupal::routeMatch()->getParameter('node')) {
  78. // if ($node instanceof \Drupal\node\NodeInterface) {
  79. // // Ajoutez une classe unique basée sur le nid (Node ID).
  80. // $variables['attributes']['class'][] = 'page-node-' . $node->id();
  81. // // Ajoutez un ID unique basé sur le nid.
  82. // $variables['attributes']['id'] = 'page-node-' . $node->id();
  83. // }
  84. // }
  85. // else {
  86. // // Si ce n'est pas une page de nœud, vous pouvez ajouter un ID basé sur le nom de la route.
  87. // $route_name = \Drupal::routeMatch()->getRouteName();
  88. // $variables['attributes']['id'] = 'page-' . str_replace('.', '-', $route_name);
  89. // }
  90. // }
  91. // /**
  92. // * Implements hook_preprocess_HOOK() for node templates.
  93. // */
  94. // function reha_preprocess_node(&$variables) {
  95. // if ($variables['node']->getType() == 'site') {
  96. // $fields_to_exclude = [
  97. // 'field--type-image', // Remplacez par le nom machine exact de votre champ image
  98. // ];
  99. // $filtered_content = [];
  100. // $image_field_content = '';
  101. // foreach ($variables['content'] as $field_name => $field_content) {
  102. // if (!in_array($field_name, $fields_to_exclude)) {
  103. // $filtered_content[$field_name] = $field_content;
  104. // } else {
  105. // $image_field_content = $field_content;
  106. // }
  107. // }
  108. // $variables['filtered_content'] = $filtered_content;
  109. // $variables['image_field_content'] = $image_field_content;
  110. // }
  111. // }
  112. /**
  113. * Prepares variables for block templates.
  114. *
  115. * Default template: block.html.twig.
  116. *
  117. * Prepares the values passed to the theme_block function to be passed
  118. * into a pluggable template engine. Uses block properties to generate a
  119. * series of template file suggestions. If none are found, the default
  120. * block.html.twig is used.
  121. *
  122. * Most themes use their own copy of block.html.twig. The default is located
  123. * inside "core/modules/block/templates/block.html.twig". Look in there for the
  124. * full list of available variables.
  125. *
  126. * @param array $variables
  127. * An associative array containing:
  128. * - elements: An associative array containing the properties of the element.
  129. * Properties used: #block, #configuration, #children, #plugin_id.
  130. */
  131. //https://www.hashbangcode.com/article/drupal-9-programmatically-creating-and-using-urls-and-links
  132. function reha_preprocess_block(&$variables) {
  133. // Conserver les IDs existants et générer un ID unique uniquement si aucun ID n'est présent
  134. if (empty($variables['attributes']['id'])) {
  135. $block_id = 'block-' . uniqid();
  136. $variables['attributes']['id'] = $block_id;
  137. }
  138. if ($variables['plugin_id'] === "user_login_block") {
  139. $url = new Url('user.register', [], ['query' => ['destination' => '/node/add/operation']]);
  140. $link = new Link('proposer une opération', $url);
  141. $variables['content']['reha'] = array(
  142. '#theme' => 'item_list',
  143. '#items' => [
  144. 'operations' => [
  145. "add_operation" => $link->toRenderable(),
  146. "description" => [
  147. "#markup" => Markup::create("<p>Créer un compte pour charger une opération</p>")
  148. ]
  149. ]
  150. ]
  151. );
  152. }
  153. if ($variables['plugin_id'] === "page_title_block") {
  154. if (\Drupal::routeMatch()->getRouteName() === 'node.add') {
  155. $node_type = \Drupal::routeMatch()->getParameter('node_type');
  156. if ($node_type->id() === 'operation') {
  157. $variables['content'] = [
  158. '#type' => 'page_title',
  159. '#title' => 'Proposer une opération'
  160. ];
  161. }
  162. }
  163. // // Extraction du texte du <h2> et transformation en ID
  164. // if (isset($variables['content']['#contextual_links']['block']['h2'])) {
  165. // $h2_text = $variables['content']['#contextual_links']['block']['h2'];
  166. // $block_id = Html::cleanCssIdentifier($h2_text);
  167. // $variables['attributes']['id'] = $block_id;
  168. // } elseif (isset($variables['content']['#block_content']['body']['0']['#text'])) {
  169. // $h2_text = $variables['content']['#block_content']['body']['0']['#text'];
  170. // $block_id = Html::cleanCssIdentifier(strip_tags($h2_text));
  171. // $variables['attributes']['id'] = $block_id;
  172. // } else {
  173. // // Fallback ID if no <h2> is found
  174. // $block_id = Html::cleanCssIdentifier($variables['plugin_id']);
  175. // $variables['attributes']['id'] = $block_id;
  176. // }
  177. }
  178. // if ($variables['plugin_id'] == 'actus-block-1') {
  179. // $fields_to_exclude = [
  180. // 'field_image', // Remplacez par le nom machine exact du champ que vous souhaitez exclure
  181. // ];
  182. // $filtered_content = [];
  183. // $excluded_field_content = '';
  184. // foreach ($variables['content'] as $field_name => $field_content) {
  185. // if (!in_array($field_name, $fields_to_exclude)) {
  186. // $filtered_content[$field_name] = $field_content;
  187. // } else {
  188. // $excluded_field_content = $field_content;
  189. // }
  190. // }
  191. // $variables['filtered_content'] = $filtered_content;
  192. // $variables['excluded_field_content'] = $excluded_field_content;
  193. // }
  194. }