reha.theme 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. use Drupal\user\Entity\User;
  7. use Drupal\block\Entity\Block;
  8. use Drupal\Core\Render\Element;
  9. /**
  10. * @file
  11. * Functions to support theming in the reha theme.
  12. */
  13. /**
  14. * Implements hook_preprocess_HOOK() for html.html.twig.
  15. */
  16. function reha_preprocess_html(&$variables) {
  17. $node = \Drupal::routeMatch()->getParameter('node');
  18. if ($node){
  19. $variables['attributes']['class'][] = 'node-type-' . $node->bundle();
  20. $variables['attributes']['class'][] = 'node-id-' . $node->id();
  21. }
  22. }
  23. /**
  24. * Implements hook_preprocess_HOOK() for node.html.twig.
  25. */
  26. function reha_preprocess_node(&$variables) {
  27. $node = &$variables['node'];
  28. $variables['attributes']['class'][] = 'node-type-' . $node->gettype();
  29. if ($node->getType() == 'site' || $node->getType() == 'ressource' || $node->getType() == 'actualite') {
  30. $fields_to_exclude = [
  31. 'field_image', // Remplacez par le nom machine exact de votre champ image
  32. ];
  33. $filtered_content = [];
  34. $image_field_content = '';
  35. $body_field_content = '';
  36. $files_field_content = '';
  37. $liens_field_content = '';
  38. $numero_field_content = '';
  39. $adresse_field_content = '';
  40. $lettre_field_content = '';
  41. $image_caption_field_content = '';
  42. foreach ($variables['content'] as $field_name => $field_content) {
  43. if ($field_name == 'body') {
  44. $body_field_content = $field_content;
  45. } elseif ($field_name == 'field_fichiers') {
  46. $files_field_content = $field_content;
  47. } elseif ($field_name == 'field_liens') {
  48. $liens_field_content = $field_content;
  49. } elseif ($field_name == 'field_adresse_site') {
  50. $numero_field_content = $field_content;
  51. } elseif ($field_name == 'field_numero_site') {
  52. $adresse_field_content = $field_content;
  53. } elseif ($field_name == 'field_lettre_de_site') {
  54. $adresse_field_content = $field_content;
  55. } elseif ($field_name == 'image_field_caption') {
  56. $image_caption_field_content = $field_content;
  57. } elseif (!in_array($field_name, $fields_to_exclude)) {
  58. $filtered_content[$field_name] = $field_content;
  59. } else {
  60. $image_field_content = $field_content;
  61. }
  62. }
  63. $variables['filtered_content'] = $filtered_content;
  64. $variables['image_field_content'] = $image_field_content;
  65. $variables['body_field_content'] = $body_field_content;
  66. $variables['files_field_content'] = $files_field_content;
  67. $variables['liens_field_content'] = $liens_field_content;
  68. $variables['adresse_field_content'] = $adresse_field_content;
  69. $variables['numero_field_content'] = $numero_field_content;
  70. $variables['field_lettre_de_site'] = $lettre_field_content;
  71. $variables['image_field_caption'] = $image_caption_field_content;
  72. }
  73. }
  74. function reha_preprocess_block(&$variables) {
  75. // Conserver les IDs existants et générer un ID unique uniquement si aucun ID n'est présent
  76. if (empty($variables['attributes']['id'])) {
  77. $block_id = 'block-' . uniqid();
  78. $variables['attributes']['id'] = $block_id;
  79. }
  80. if ($variables['plugin_id'] === "user_login_block") {
  81. $url = new Url('user.register', [], ['query' => ['destination' => '/node/add/operation']]);
  82. $link = new Link('proposer une opération', $url);
  83. $variables['content']['reha'] = array(
  84. '#theme' => 'item_list',
  85. '#items' => [
  86. 'operations' => [
  87. "add_operation" => $link->toRenderable(),
  88. "description" => [
  89. "#markup" => Markup::create("<p>Créer un compte pour charger une opération</p>")
  90. ]
  91. ]
  92. ]
  93. );
  94. }
  95. if ($variables['plugin_id'] === "page_title_block") {
  96. if (\Drupal::routeMatch()->getRouteName() === 'node.add') {
  97. $node_type = \Drupal::routeMatch()->getParameter('node_type');
  98. if ($node_type->id() === 'operation') {
  99. $variables['content'] = [
  100. '#type' => 'page_title',
  101. '#title' => 'Proposer une opération'
  102. ];
  103. }
  104. }
  105. }
  106. if ($variables['plugin_id'] === "views_block:current_user_block-block_1") {
  107. $user = User::load($variables['user']->id());
  108. $prenomnom = $user->get('field_prenom')->getString() . ' ' . $user->get('field_nom')->getString();
  109. $variables['content']['#title']['#markup'] = $prenomnom;
  110. $variables['label']['#markup'] = $prenomnom;
  111. }
  112. }
  113. function reha_preprocess_field(&$variables){
  114. if($variables['field_name'] === 'field_adresse_site'){
  115. foreach($variables['items'] as $index => $adr){
  116. if (isset($variables['items'][$index]['content']['postal_code'])) {
  117. $postal_code = $variables['items'][$index]['content']['postal_code']['#value'];
  118. $pattern = '/(\d{2})(\d+)/i';
  119. $replacement = '($1)';
  120. $variables['items'][$index]['content']['postal_code']['#value'] = preg_replace($pattern, $replacement, $postal_code);
  121. }
  122. }
  123. }
  124. if($variables['field_name'] === 'field_image'){
  125. if($variables['element']['#view_mode'] === 'home_block'){
  126. array_splice($variables['items'], 1);
  127. }
  128. }
  129. }