eql.theme 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /*
  3. * @files
  4. * Theme Template Funtions
  5. */
  6. // use Drupal\Core\Template\Attribute;
  7. // use Drupal\Core\Link;
  8. // use Drupal\Core\Url;
  9. use Drupal\Component\Utility\Html;
  10. use Drupal\Component\Transliteration\TransliterationInterface;
  11. use Drupal\taxonomy\Entity\Term;
  12. use Drupal\Core\Template\Attribute;
  13. function eql_preprocess_html(&$variables) {
  14. $node = \Drupal::routeMatch()->getParameter('node');
  15. if ($node){
  16. $variables['attributes']['class'][] = 'node-type-' . $node->bundle();
  17. $variables['attributes']['class'][] = 'node-id-' . $node->id();
  18. }
  19. }
  20. function eql_preprocess_node(&$variables){
  21. $node = &$variables['node'];
  22. $variables['attributes']['class'][] = 'node-type-' . $node->gettype();
  23. if($node->gettype() == "actualite") {
  24. $actu_type = $node->get('field_actu_type')->getString();
  25. $variables['attributes']['class'][] = 'actu-type-' . $actu_type;
  26. }
  27. if($node->gettype() == "offre_de_service") {
  28. $type_protagoniste = $node->get('field_type_de_protagoniste')->getString();
  29. $variables['attributes']['class'][] = 'type-de-protagoniste-' . $type_protagoniste;
  30. }
  31. // if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  32. // // Récupère la valeur de field_type_de_ressource
  33. // $type_de_ressource_entity = $node->get('field_type_de_ressource')->entity;
  34. // if ($type_de_ressource_entity) {
  35. // $type_de_ressource_name = $type_de_ressource_entity->getName();
  36. // // Ajoute la classe CSS basée sur la valeur du type de ressource
  37. // $variables['attributes']['class'][] = 'type-de-ressource-' . Html::cleanCssIdentifier($type_de_ressource_name);
  38. // }
  39. // }
  40. if ($node->getType() === 'ressource' && $node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  41. foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  42. $label = $term->label(); // ex: "Presse"
  43. $class = 'type-' . Html::cleanCssIdentifier(strtolower($label));
  44. $variables['attributes']['class'][] = $class;
  45. }
  46. }
  47. }
  48. function eql_preprocess_field(&$variables) {
  49. $node = \Drupal::routeMatch()->getParameter('node');
  50. $field_name = &$variables['field_name'];
  51. if($node) {
  52. if ($field_name == 'field_type_de_protagoniste'){
  53. $type_protagoniste = $node->get('field_type_de_protagoniste')->getString();
  54. $variables['attributes']['class'][] = 'type-de-protagoniste-' . $type_protagoniste;
  55. }
  56. }
  57. $field_name = &$variables['field_name'];
  58. if($node) {
  59. if ($field_name == 'field-type-de-ressource'){
  60. $type_ressource = $node->get('field-type-de-ressource')->getString();
  61. $variables['attributes']['class'][] = 'type-de-ressource-' . $type_ressource;
  62. }
  63. }
  64. }
  65. ////////////
  66. function eql_preprocess_layout__threecol_25_50_25(&$variables) {
  67. $node = \Drupal::routeMatch()->getParameter('node');
  68. if($node->gettype() == "projet"){
  69. $variables['content']['top_bottom'] = []; /////// déclare les nvx array
  70. $variables['content']['top_right'] = [];
  71. $variables['content']['top_left'] = [];
  72. $variables['content']['top_bottom_left'] = [];
  73. foreach ($variables['content']['top'] as $key => $value) {
  74. if(isset($value['#base_plugin_id']) && $value['#base_plugin_id']== 'entity_field'){ ////// isset pour voir si existe && si oui fait ce que tu as à faire. $Value c'est l'attribut
  75. if($value['#derivative_plugin_id']=="node:field_region" || $value['#derivative_plugin_id']=="node:field_adresse") {
  76. $variables['content']['top_bottom_left'][] = $variables['content']['top'][$key];
  77. unset($variables["content"]["top"][$key]);
  78. }
  79. if($value['#derivative_plugin_id']=="node:field_type_de_moa" || $value['#derivative_plugin_id']=="node:field_type_de_projet" || $value['#derivative_plugin_id']=="node:field_etape_du_projet") {
  80. $variables['content']['top_bottom'][] = $variables['content']['top'][$key];
  81. unset($variables["content"]["top"][$key]);
  82. }
  83. if($value['#derivative_plugin_id']=="node:title" || $value['#derivative_plugin_id']=="node:field_equipe" || $value['#derivative_plugin_id']=="node:field_incube"){
  84. $variables['content']['top_right'][] = $variables['content']['top'][$key];
  85. unset($variables["content"]["top"][$key]);
  86. }
  87. if($value['#derivative_plugin_id']=="node:field_photo"){
  88. $variables['content']['top_left'][] = $variables['content']['top'][$key];
  89. unset($variables["content"]["top"][$key]);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. // function eql_preprocess_views_view_unformatted(&$variables) {
  96. // // On utilise le service de translittération de Drupal
  97. // $transliterator = \Drupal::service('transliteration');
  98. // foreach ($variables['rows'] as &$row) {
  99. // if (isset($row['content']['#node'])) {
  100. // $node = $row['content']['#node'];
  101. // if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  102. // foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  103. // $label = $term->label();
  104. // // Supprimer les accents
  105. // $label_ascii = $transliterator->transliterate($label);
  106. // // Nettoyer pour que ce soit une classe CSS valide
  107. // $class = 'type-' . Html::cleanCssIdentifier(strtolower($label_ascii));
  108. // $row['attributes']->addClass($class);
  109. // }
  110. // }
  111. // }
  112. // }
  113. // }
  114. function eql_preprocess_views_view_unformatted(array &$variables) {
  115. $view = $variables['view'];
  116. if ($view->id() !== 'base_de_donnees') {
  117. return;
  118. }
  119. $transliterator = \Drupal::service('transliteration');
  120. $current_path = \Drupal::service('path.current')->getPath();
  121. $request = \Drupal::request();
  122. $query_params = $request->query->all();
  123. // Active uniquement si on est sur la page /ressources sans filtres actifs
  124. $is_ressource_main = $current_path === '/ressources';
  125. $has_filters = !empty(array_filter($query_params, fn($v) => $v !== '' && $v !== 'All'));
  126. $filter_slides = $is_ressource_main && !$has_filters;
  127. // Liste des types à restreindre uniquement sur /ressources sans filtres
  128. $allowed_types = [
  129. 'type-paroles-de-laureats',
  130. 'type-publication-issue-du-programme-eqld',
  131. 'type-presse',
  132. 'type-projets-en-images',
  133. ];
  134. $slides = [];
  135. $unclassified_rows = [];
  136. foreach ($variables['rows'] as $row) {
  137. if (!isset($row['content']['#node'])) {
  138. $unclassified_rows[] = $row;
  139. continue;
  140. }
  141. $node = $row['content']['#node'];
  142. $matched = false;
  143. if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  144. foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  145. $label = $term->label();
  146. $label_ascii = $transliterator->transliterate($label);
  147. $type_class = 'type-' . Html::cleanCssIdentifier(strtolower($label_ascii));
  148. // Init slide si non encore créé
  149. if (!isset($slides[$type_class])) {
  150. $slides[$type_class] = [
  151. 'label' => $label,
  152. 'rows' => [],
  153. ];
  154. }
  155. // Ajouter la row dans le slide
  156. $slides[$type_class]['rows'][] = $row;
  157. $matched = true;
  158. // Ajouter la classe CSS à la row
  159. $row['attributes']->addClass($type_class);
  160. // Ajouter aussi à l'article si possible
  161. if (!isset($row['content']['#attributes']) || !($row['content']['#attributes'] instanceof Attribute)) {
  162. $row['content']['#attributes'] = new Attribute();
  163. }
  164. $row['content']['#attributes']->addClass($type_class);
  165. }
  166. }
  167. if (!$matched) {
  168. $unclassified_rows[] = $row;
  169. }
  170. }
  171. // Appliquer le filtrage final si sur la page principale
  172. $variables['grouped_rows'] = $filter_slides
  173. ? array_filter($slides, fn($key) => in_array($key, $allowed_types), ARRAY_FILTER_USE_KEY)
  174. : $slides;
  175. $variables['unclassified_rows'] = $unclassified_rows;
  176. $variables['rows'] = []; // désactive affichage par défaut
  177. }