reha.theme 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. foreach ($variables['content'] as $field_name => $field_content) {
  40. if ($field_name == 'body') {
  41. $body_field_content = $field_content;
  42. } elseif ($field_name == 'field_fichiers') {
  43. $files_field_content = $field_content;
  44. } elseif ($field_name == 'field_liens') {
  45. $liens_field_content = $field_content;
  46. } elseif ($field_name == 'field_adresse_site') {
  47. $numero_field_content = $field_content;
  48. } elseif ($field_name == 'field_numero_site') {
  49. $adresse_field_content = $field_content;
  50. } elseif ($field_name == 'field_lettre_de_site') {
  51. $adresse_field_content = $field_content;
  52. } elseif (!in_array($field_name, $fields_to_exclude)) {
  53. $filtered_content[$field_name] = $field_content;
  54. } else {
  55. $image_field_content = $field_content;
  56. }
  57. }
  58. $variables['filtered_content'] = $filtered_content;
  59. $variables['image_field_content'] = $image_field_content;
  60. $variables['body_field_content'] = $body_field_content;
  61. $variables['files_field_content'] = $files_field_content;
  62. $variables['liens_field_content'] = $liens_field_content;
  63. $variables['adresse_field_content'] = $adresse_field_content;
  64. $variables['numero_field_content'] = $numero_field_content;
  65. $variables['field_lettre_de_site'] = $lettre_field_content;
  66. }
  67. }
  68. function reha_preprocess_block(&$variables) {
  69. // Conserver les IDs existants et générer un ID unique uniquement si aucun ID n'est présent
  70. if (empty($variables['attributes']['id'])) {
  71. $block_id = 'block-' . uniqid();
  72. $variables['attributes']['id'] = $block_id;
  73. }
  74. if ($variables['plugin_id'] === "user_login_block") {
  75. $url = new Url('user.register', [], ['query' => ['destination' => '/node/add/operation']]);
  76. $link = new Link('proposer une opération', $url);
  77. $variables['content']['reha'] = array(
  78. '#theme' => 'item_list',
  79. '#items' => [
  80. 'operations' => [
  81. "add_operation" => $link->toRenderable(),
  82. "description" => [
  83. "#markup" => Markup::create("<p>Créer un compte pour charger une opération</p>")
  84. ]
  85. ]
  86. ]
  87. );
  88. }
  89. if ($variables['plugin_id'] === "page_title_block") {
  90. if (\Drupal::routeMatch()->getRouteName() === 'node.add') {
  91. $node_type = \Drupal::routeMatch()->getParameter('node_type');
  92. if ($node_type->id() === 'operation') {
  93. $variables['content'] = [
  94. '#type' => 'page_title',
  95. '#title' => 'Proposer une opération'
  96. ];
  97. }
  98. }
  99. }
  100. if ($variables['plugin_id'] === "views_block:current_user_block-block_1") {
  101. $user = User::load($variables['user']->id());
  102. $prenomnom = $user->get('field_prenom')->getString() . ' ' . $user->get('field_nom')->getString();
  103. $variables['content']['#title']['#markup'] = $prenomnom;
  104. $variables['label']['#markup'] = $prenomnom;
  105. }
  106. }
  107. function reha_preprocess_field(&$variables){
  108. if($variables['field_name'] === 'field_adresse_site'){
  109. foreach($variables['items'] as $index => $adr){
  110. if (isset($variables['items'][$index]['content']['postal_code'])) {
  111. $postal_code = $variables['items'][$index]['content']['postal_code']['#value'];
  112. $pattern = '/(\d{2})(\d+)/i';
  113. $replacement = '($1)';
  114. $variables['items'][$index]['content']['postal_code']['#value'] = preg_replace($pattern, $replacement, $postal_code);
  115. }
  116. }
  117. }
  118. if($variables['field_name'] === 'field_image'){
  119. if($variables['element']['#view_mode'] === 'home_block'){
  120. array_splice($variables['items'], 1);
  121. }
  122. }
  123. }