reha.theme 4.2 KB

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