reha.theme 6.1 KB

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