eql.theme 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. if ($node instanceof NodeInterface) {
  20. $variables['attributes']['class'][] = 'node-type-' . $node->bundle();
  21. $variables['attributes']['class'][] = 'node-id-' . $node->id();
  22. // Pour les ressources : ajoute la classe de type de ressource
  23. if ($node->bundle() === 'ressource' && $node->hasField('field_type_de_ressource')) {
  24. foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  25. $label = $term->label();
  26. $css_class = 'type-' . Html::getClass($label);
  27. $variables['attributes']['class'][] = $css_class;
  28. }
  29. }
  30. }
  31. }
  32. function eql_preprocess_node(&$variables){
  33. $node = &$variables['node'];
  34. $variables['attributes']['class'][] = 'node-type-' . $node->gettype();
  35. if($node->gettype() == "actualite") {
  36. $actu_type = $node->get('field_actu_type')->getString();
  37. $variables['attributes']['class'][] = 'actu-type-' . $actu_type;
  38. }
  39. if($node->gettype() == "offre_de_service") {
  40. $type_protagoniste = $node->get('field_type_de_protagoniste')->getString();
  41. $variables['attributes']['class'][] = 'type-de-protagoniste-' . $type_protagoniste;
  42. }
  43. if ($node->getType() === 'ressource' && $node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  44. foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  45. $label = $term->label(); // ex: "Presse"
  46. $class = 'type-' . Html::cleanCssIdentifier(strtolower($label));
  47. $variables['attributes']['class'][] = $class;
  48. }
  49. }
  50. }
  51. function eql_preprocess_page(array &$variables) {
  52. $node = \Drupal::routeMatch()->getParameter('node');
  53. if ($node instanceof \Drupal\node\NodeInterface && $node->bundle() === 'ressource') {
  54. if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  55. foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  56. $label = $term->label();
  57. $css_class = 'type-' . \Drupal\Component\Utility\Html::getClass($label);
  58. // S'assurer que les attributs existent
  59. if (!isset($variables['page']['top']['#attributes'])) {
  60. $variables['page']['top']['#attributes'] = ['class' => []];
  61. }
  62. $variables['page']['top']['#attributes']['class'][] = $css_class;
  63. }
  64. }
  65. }
  66. }
  67. function eql_preprocess_block(array &$variables) {
  68. // Vérifie si on est sur une page node de type 'ressource'
  69. $route_match = \Drupal::routeMatch();
  70. $node = $route_match->getParameter('node');
  71. if ($node instanceof \Drupal\node\NodeInterface && $node->bundle() === 'ressource') {
  72. $variables['is_ressource_page'] = TRUE;
  73. }
  74. else {
  75. $variables['is_ressource_page'] = FALSE;
  76. }
  77. }
  78. function eql_preprocess_field(&$variables) {
  79. $node = \Drupal::routeMatch()->getParameter('node');
  80. $field_name = &$variables['field_name'];
  81. if($node) {
  82. if ($field_name == 'field_type_de_protagoniste'){
  83. $type_protagoniste = $node->get('field_type_de_protagoniste')->getString();
  84. $variables['attributes']['class'][] = 'type-de-protagoniste-' . $type_protagoniste;
  85. }
  86. }
  87. $field_name = &$variables['field_name'];
  88. if($node) {
  89. if ($field_name == 'field-type-de-ressource'){
  90. $type_ressource = $node->get('field-type-de-ressource')->getString();
  91. $variables['attributes']['class'][] = 'type-de-ressource-' . $type_ressource;
  92. }
  93. }
  94. }
  95. ////////////
  96. function eql_preprocess_layout__threecol_25_50_25(&$variables) {
  97. $node = \Drupal::routeMatch()->getParameter('node');
  98. if($node->gettype() == "projet"){
  99. $variables['content']['top_bottom'] = []; /////// déclare les nvx array
  100. $variables['content']['top_right'] = [];
  101. $variables['content']['top_left'] = [];
  102. $variables['content']['top_bottom_left'] = [];
  103. foreach ($variables['content']['top'] as $key => $value) {
  104. 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
  105. if($value['#derivative_plugin_id']=="node:field_region" || $value['#derivative_plugin_id']=="node:field_adresse") {
  106. $variables['content']['top_bottom_left'][] = $variables['content']['top'][$key];
  107. unset($variables["content"]["top"][$key]);
  108. }
  109. 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") {
  110. $variables['content']['top_bottom'][] = $variables['content']['top'][$key];
  111. unset($variables["content"]["top"][$key]);
  112. }
  113. if($value['#derivative_plugin_id']=="node:title" || $value['#derivative_plugin_id']=="node:field_equipe" || $value['#derivative_plugin_id']=="node:field_incube"){
  114. $variables['content']['top_right'][] = $variables['content']['top'][$key];
  115. unset($variables["content"]["top"][$key]);
  116. }
  117. if($value['#derivative_plugin_id']=="node:field_photo"){
  118. $variables['content']['top_left'][] = $variables['content']['top'][$key];
  119. unset($variables["content"]["top"][$key]);
  120. }
  121. }
  122. }
  123. }
  124. if ($node && $node->getType() === 'ressource') {
  125. if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  126. $terms = $node->get('field_type_de_ressource')->referencedEntities();
  127. if (!empty($terms)) {
  128. $label = $terms[0]->label();
  129. $css_class = 'type-' . Html::cleanCssIdentifier(strtolower($label));
  130. // Appliquer cette classe à la région "top"
  131. if (!isset($variables['region_attributes']['top'])) {
  132. $variables['region_attributes']['top'] = new \Drupal\Core\Template\Attribute();
  133. }
  134. $variables['region_attributes']['top']->addClass($css_class);
  135. }
  136. }
  137. }
  138. }
  139. function eql_preprocess_views_view_unformatted(array &$variables) {
  140. $view = $variables['view'];
  141. if ($view->id() !== 'base_de_donnees') {
  142. return;
  143. }
  144. $transliterator = \Drupal::service('transliteration');
  145. $current_path = \Drupal::service('path.current')->getPath();
  146. $request = \Drupal::request();
  147. $query_params = $request->query->all();
  148. // Active uniquement si on est sur la page /ressources sans filtres actifs
  149. $is_ressource_main = $current_path === '/ressources';
  150. $has_filters = !empty(array_filter($query_params, fn($v) => $v !== '' && $v !== 'All'));
  151. $filter_slides = $is_ressource_main && !$has_filters;
  152. // Liste des types à restreindre uniquement sur /ressources sans filtres
  153. $allowed_types = [
  154. 'type-paroles-de-laureats',
  155. 'type-publication-issue-du-programme-eqld',
  156. 'type-presse',
  157. 'type-les-projets-en-images',
  158. ];
  159. $slides = [];
  160. $unclassified_rows = [];
  161. foreach ($variables['rows'] as $row) {
  162. if (!isset($row['content']['#node'])) {
  163. $unclassified_rows[] = $row;
  164. continue;
  165. }
  166. $node = $row['content']['#node'];
  167. $matched = false;
  168. if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  169. foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  170. $label = $term->label();
  171. $label_ascii = $transliterator->transliterate($label);
  172. $type_class = 'type-' . Html::cleanCssIdentifier(strtolower($label_ascii));
  173. // Init slide si non encore créé
  174. if (!isset($slides[$type_class])) {
  175. $slides[$type_class] = [
  176. 'label' => $label,
  177. 'rows' => [],
  178. ];
  179. }
  180. // Ajouter la row dans le slide
  181. $slides[$type_class]['rows'][] = $row;
  182. $matched = true;
  183. // Ajouter la classe CSS à la row
  184. $row['attributes']->addClass($type_class);
  185. // Ajouter aussi à l'article si possible
  186. if (!isset($row['content']['#attributes']) || !($row['content']['#attributes'] instanceof Attribute)) {
  187. $row['content']['#attributes'] = new Attribute();
  188. }
  189. $row['content']['#attributes']->addClass($type_class);
  190. }
  191. }
  192. if (!$matched) {
  193. $unclassified_rows[] = $row;
  194. }
  195. }
  196. // Appliquer le filtrage final si sur la page principale
  197. $variables['grouped_rows'] = $filter_slides
  198. ? array_filter($slides, fn($key) => in_array($key, $allowed_types), ARRAY_FILTER_USE_KEY)
  199. : $slides;
  200. $variables['unclassified_rows'] = $unclassified_rows;
  201. $variables['rows'] = []; // désactive affichage par défaut
  202. }