eql.theme 9.8 KB

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