eql.theme 11 KB

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