reha.theme 4.7 KB

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