eql.theme 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. use Drupal\node\NodeInterface;
  14. function eql_preprocess_html(&$variables) {
  15. $request = \Drupal::request();
  16. if ($request->query->get('print') === '1') {
  17. $variables['#attached']['library'][] = 'eql/paged_print';
  18. $variables['attributes']['class'][] = 'use-paged';
  19. }
  20. $node = \Drupal::routeMatch()->getParameter('node');
  21. if ($node instanceof \Drupal\node\NodeInterface) {
  22. $variables['attributes']['class'][] = 'node-type-' . $node->bundle();
  23. $variables['attributes']['class'][] = 'node-id-' . $node->id();
  24. // Liste des champs à traiter pour générer des classes dynamiques
  25. $fields_to_classify = [
  26. 'field_type_de_ressource' => 'type-',
  27. 'field_type_de_media' => 'type-media-',
  28. ];
  29. foreach ($fields_to_classify as $field_name => $prefix) {
  30. if ($node->hasField($field_name) && !$node->get($field_name)->isEmpty()) {
  31. foreach ($node->get($field_name)->referencedEntities() as $term) {
  32. $label = $term->label();
  33. $css_class = $prefix . Html::cleanCssIdentifier(strtolower($label));
  34. $variables['attributes']['class'][] = $css_class;
  35. }
  36. }
  37. }
  38. }
  39. }
  40. function eql_preprocess_node(&$variables){
  41. $node = &$variables['node'];
  42. $variables['attributes']['class'][] = 'node-type-' . $node->gettype();
  43. if($node->gettype() == "actualite") {
  44. $actu_type = $node->get('field_actu_type')->getString();
  45. $variables['attributes']['class'][] = 'actu-type-' . $actu_type;
  46. }
  47. if($node->gettype() == "offre_de_service") {
  48. $type_protagoniste = $node->get('field_type_de_protagoniste')->getString();
  49. $variables['attributes']['class'][] = 'type-de-protagoniste-' . $type_protagoniste;
  50. }
  51. if ($node->getType() === 'ressource' && $node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  52. foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  53. $label = $term->label(); // ex: "Presse"
  54. $class = 'type-' . Html::cleanCssIdentifier(strtolower($label));
  55. $variables['attributes']['class'][] = $class;
  56. }
  57. }
  58. }
  59. function eql_preprocess_page(array &$variables) {
  60. $node = \Drupal::routeMatch()->getParameter('node');
  61. if ($node instanceof \Drupal\node\NodeInterface && $node->bundle() === 'ressource') {
  62. if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  63. foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  64. $label = $term->label();
  65. $css_class = 'type-' . \Drupal\Component\Utility\Html::getClass($label);
  66. // S'assurer que les attributs existent
  67. if (!isset($variables['page']['top']['#attributes'])) {
  68. $variables['page']['top']['#attributes'] = ['class' => []];
  69. }
  70. $variables['page']['top']['#attributes']['class'][] = $css_class;
  71. }
  72. }
  73. }
  74. }
  75. function eql_preprocess_block(array &$variables) {
  76. // Vérifie si on est sur une page node de type 'ressource'
  77. $route_match = \Drupal::routeMatch();
  78. $node = $route_match->getParameter('node');
  79. if ($node instanceof \Drupal\node\NodeInterface && $node->bundle() === 'ressource') {
  80. $variables['is_ressource_page'] = TRUE;
  81. }
  82. else {
  83. $variables['is_ressource_page'] = FALSE;
  84. }
  85. }
  86. function eql_preprocess_field(&$variables) {
  87. $node = \Drupal::routeMatch()->getParameter('node');
  88. $field_name = &$variables['field_name'];
  89. if($node) {
  90. if ($field_name == 'field_type_de_protagoniste'){
  91. $type_protagoniste = $node->get('field_type_de_protagoniste')->getString();
  92. $variables['attributes']['class'][] = 'type-de-protagoniste-' . $type_protagoniste;
  93. }
  94. }
  95. $field_name = &$variables['field_name'];
  96. if($node) {
  97. if ($field_name == 'field-type-de-ressource'){
  98. $type_ressource = $node->get('field-type-de-ressource')->getString();
  99. $variables['attributes']['class'][] = 'type-de-ressource-' . $type_ressource;
  100. }
  101. }
  102. }
  103. ////////////
  104. function eql_preprocess_layout__threecol_25_50_25(&$variables) {
  105. $node = \Drupal::routeMatch()->getParameter('node');
  106. if($node->gettype() == "projet"){
  107. $variables['content']['top_bottom'] = []; /////// déclare les nvx array
  108. $variables['content']['top_right'] = [];
  109. $variables['content']['top_left'] = [];
  110. $variables['content']['top_bottom_left'] = [];
  111. foreach ($variables['content']['top'] as $key => $value) {
  112. 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
  113. if($value['#derivative_plugin_id']=="node:field_region" || $value['#derivative_plugin_id']=="node:field_adresse") {
  114. $variables['content']['top_bottom_left'][] = $variables['content']['top'][$key];
  115. unset($variables["content"]["top"][$key]);
  116. }
  117. 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") {
  118. $variables['content']['top_bottom'][] = $variables['content']['top'][$key];
  119. unset($variables["content"]["top"][$key]);
  120. }
  121. if($value['#derivative_plugin_id']=="node:title" || $value['#derivative_plugin_id']=="node:field_equipe" || $value['#derivative_plugin_id']=="node:field_incube"){
  122. $variables['content']['top_right'][] = $variables['content']['top'][$key];
  123. unset($variables["content"]["top"][$key]);
  124. }
  125. if($value['#derivative_plugin_id']=="node:field_photo"){
  126. $variables['content']['top_left'][] = $variables['content']['top'][$key];
  127. unset($variables["content"]["top"][$key]);
  128. }
  129. }
  130. }
  131. }
  132. if ($node && $node->getType() === 'ressource') {
  133. if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  134. $terms = $node->get('field_type_de_ressource')->referencedEntities();
  135. if (!empty($terms)) {
  136. foreach ($terms as $term) {
  137. if ($term instanceof \Drupal\taxonomy\Entity\Term) {
  138. $label = $term->label();
  139. $css_class = 'type-' . \Drupal\Component\Utility\Html::cleanCssIdentifier(strtolower($label));
  140. if (!isset($variables['region_attributes']['top'])) {
  141. $variables['region_attributes']['top'] = new \Drupal\Core\Template\Attribute();
  142. }
  143. $variables['region_attributes']['top']->addClass($css_class);
  144. }
  145. }
  146. }
  147. }
  148. // Champ : field_type_de_media
  149. if ($node->hasField('field_type_de_media') && !$node->get('field_type_de_media')->isEmpty()) {
  150. $terms = $node->get('field_type_de_media')->referencedEntities();
  151. foreach ($terms as $term) {
  152. if ($term instanceof \Drupal\taxonomy\Entity\Term) {
  153. $media_label = $term->label();
  154. $css_class = 'type-media-' . \Drupal\Component\Utility\Html::cleanCssIdentifier(strtolower($media_label));
  155. if (!isset($variables['region_attributes']['top'])) {
  156. $variables['region_attributes']['top'] = new \Drupal\Core\Template\Attribute();
  157. }
  158. $variables['region_attributes']['top']->addClass($css_class);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. function eql_preprocess_views_view_unformatted(array &$variables) {
  165. $view = $variables['view'];
  166. if ($view->id() !== 'base_de_donnees') {
  167. return;
  168. }
  169. $transliterator = \Drupal::service('transliteration');
  170. $current_path = \Drupal::service('path.current')->getPath();
  171. $request = \Drupal::request();
  172. $query_params = $request->query->all();
  173. // Active uniquement si on est sur la page /ressources sans filtres actifs
  174. $is_ressource_main = $current_path === '/ressources';
  175. $has_filters = !empty(array_filter($query_params, fn($v) => $v !== '' && $v !== 'All'));
  176. $filter_slides = $is_ressource_main && !$has_filters;
  177. // Liste des types à restreindre uniquement sur /ressources sans filtres
  178. $allowed_types = [
  179. 'type-paroles-de-laureats',
  180. 'type-publications-issues-du-programme',
  181. 'type-presse',
  182. 'type-les-projets-en-images',
  183. // 'type-autres',
  184. // 'type-evenements',
  185. 'type-publications-des-partenaires-et-laureats',
  186. ];
  187. $slides = [];
  188. $unclassified_rows = [];
  189. foreach ($variables['rows'] as $row) {
  190. if (!isset($row['content']['#node'])) {
  191. $unclassified_rows[] = $row;
  192. continue;
  193. }
  194. $node = $row['content']['#node'];
  195. $matched = false;
  196. if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  197. foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  198. $label = $term->label();
  199. $tid = $term->id(); // <-- on récupère l'ID
  200. $label_ascii = $transliterator->transliterate($label);
  201. $type_class = 'type-' . Html::cleanCssIdentifier(strtolower($label_ascii));
  202. // Init slide si non encore créé
  203. if (!isset($slides[$type_class])) {
  204. $slides[$type_class] = [
  205. 'label' => $label,
  206. 'tid' => $term->id(),
  207. 'rows' => [],
  208. ];
  209. }
  210. // Ajouter la row dans le slide
  211. $slides[$type_class]['rows'][] = $row;
  212. $matched = true;
  213. // Ajouter la classe CSS à la row
  214. $row['attributes']->addClass($type_class);
  215. // Ajouter aussi à l'article si possible
  216. if (!isset($row['content']['#attributes']) || !($row['content']['#attributes'] instanceof Attribute)) {
  217. $row['content']['#attributes'] = new Attribute();
  218. }
  219. $row['content']['#attributes']->addClass($type_class);
  220. }
  221. }
  222. if (!$matched) {
  223. $unclassified_rows[] = $row;
  224. }
  225. }
  226. // Appliquer le filtrage final si sur la page principale
  227. $variables['grouped_rows'] = $filter_slides
  228. ? array_filter($slides, fn($key) => in_array($key, $allowed_types), ARRAY_FILTER_USE_KEY)
  229. : $slides;
  230. $variables['unclassified_rows'] = $unclassified_rows;
  231. $variables['rows'] = []; // désactive affichage par défaut
  232. $variables['is_filtered'] = !$filter_slides;
  233. }
  234. function eql_preprocess_form(&$variables) {
  235. $terms = \Drupal::entityTypeManager()
  236. ->getStorage('taxonomy_term')
  237. ->loadTree('type_de_media');
  238. $transliterator = \Drupal::service('transliteration');
  239. $media_links = [];
  240. // Récupère la valeur actuelle du filtre dans l’URL
  241. $request = \Drupal::request();
  242. $active_tid = $request->query->get('field_type_de_media_target_id');
  243. foreach ($terms as $term) {
  244. $slug = Html::cleanCssIdentifier(strtolower($transliterator->transliterate($term->name)));
  245. $slug = Html::cleanCssIdentifier($slug); // Nettoie pour CSS
  246. $media_links[] = [
  247. 'tid' => $term->tid,
  248. 'label' => $term->name,
  249. 'slug' => $slug,
  250. 'active' => (string)$term->tid === $active_tid, // <-- True si c'est celui sélectionné
  251. ];
  252. }
  253. $variables['media_links'] = $media_links;
  254. }