eql.theme 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. foreach ($terms as $term) {
  129. if ($term instanceof \Drupal\taxonomy\Entity\Term) {
  130. $label = $term->label();
  131. $css_class = 'type-' . \Drupal\Component\Utility\Html::cleanCssIdentifier(strtolower($label));
  132. if (!isset($variables['region_attributes']['top'])) {
  133. $variables['region_attributes']['top'] = new \Drupal\Core\Template\Attribute();
  134. }
  135. $variables['region_attributes']['top']->addClass($css_class);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. function eql_preprocess_views_view_unformatted(array &$variables) {
  143. $view = $variables['view'];
  144. if ($view->id() !== 'base_de_donnees') {
  145. return;
  146. }
  147. $transliterator = \Drupal::service('transliteration');
  148. $current_path = \Drupal::service('path.current')->getPath();
  149. $request = \Drupal::request();
  150. $query_params = $request->query->all();
  151. // Active uniquement si on est sur la page /ressources sans filtres actifs
  152. $is_ressource_main = $current_path === '/ressources';
  153. $has_filters = !empty(array_filter($query_params, fn($v) => $v !== '' && $v !== 'All'));
  154. $filter_slides = $is_ressource_main && !$has_filters;
  155. // Liste des types à restreindre uniquement sur /ressources sans filtres
  156. $allowed_types = [
  157. 'type-paroles-de-laureats',
  158. 'type-publication-issue-du-programme-eqld',
  159. 'type-presse',
  160. 'type-les-projets-en-images',
  161. ];
  162. $slides = [];
  163. $unclassified_rows = [];
  164. foreach ($variables['rows'] as $row) {
  165. if (!isset($row['content']['#node'])) {
  166. $unclassified_rows[] = $row;
  167. continue;
  168. }
  169. $node = $row['content']['#node'];
  170. $matched = false;
  171. if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
  172. foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
  173. $label = $term->label();
  174. $label_ascii = $transliterator->transliterate($label);
  175. $type_class = 'type-' . Html::cleanCssIdentifier(strtolower($label_ascii));
  176. // Init slide si non encore créé
  177. if (!isset($slides[$type_class])) {
  178. $slides[$type_class] = [
  179. 'label' => $label,
  180. 'rows' => [],
  181. ];
  182. }
  183. // Ajouter la row dans le slide
  184. $slides[$type_class]['rows'][] = $row;
  185. $matched = true;
  186. // Ajouter la classe CSS à la row
  187. $row['attributes']->addClass($type_class);
  188. // Ajouter aussi à l'article si possible
  189. if (!isset($row['content']['#attributes']) || !($row['content']['#attributes'] instanceof Attribute)) {
  190. $row['content']['#attributes'] = new Attribute();
  191. }
  192. $row['content']['#attributes']->addClass($type_class);
  193. }
  194. }
  195. if (!$matched) {
  196. $unclassified_rows[] = $row;
  197. }
  198. }
  199. // Appliquer le filtrage final si sur la page principale
  200. $variables['grouped_rows'] = $filter_slides
  201. ? array_filter($slides, fn($key) => in_array($key, $allowed_types), ARRAY_FILTER_USE_KEY)
  202. : $slides;
  203. $variables['unclassified_rows'] = $unclassified_rows;
  204. $variables['rows'] = []; // désactive affichage par défaut
  205. }