reha.theme 5.3 KB

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