php class

This commit is contained in:
2025-05-23 09:10:23 +02:00
parent b0c93b3eef
commit 86aaef30fc
9 changed files with 227 additions and 121 deletions
+101 -18
View File
@@ -9,7 +9,8 @@
// use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Template\Attribute;
function eql_preprocess_html(&$variables) {
$node = \Drupal::routeMatch()->getParameter('node');
@@ -41,13 +42,13 @@ function eql_preprocess_node(&$variables){
// $variables['attributes']['class'][] = 'type-de-ressource-' . Html::cleanCssIdentifier($type_de_ressource_name);
// }
// }
if ($node->getType() === 'ressource' && $node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
$label = $term->label(); // ex: "Presse"
$class = 'type-' . Html::cleanCssIdentifier(strtolower($label));
$variables['attributes']['class'][] = $class;
}
}
// if ($node->getType() === 'ressource' && $node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
// foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
// $label = $term->label(); // ex: "Presse"
// $class = 'type-' . Html::cleanCssIdentifier(strtolower($label));
// $variables['attributes']['class'][] = $class;
// }
// }
}
function eql_preprocess_field(&$variables) {
@@ -104,29 +105,111 @@ function eql_preprocess_layout__threecol_25_50_25(&$variables) {
function eql_preprocess_views_view_unformatted(&$variables) {
// On utilise le service de translittération de Drupal
$transliterator = \Drupal::service('transliteration');
foreach ($variables['rows'] as &$row) {
if (isset($row['content']['#node'])) {
// function eql_preprocess_views_view_unformatted(&$variables) {
// // On utilise le service de translittération de Drupal
// $transliterator = \Drupal::service('transliteration');
// foreach ($variables['rows'] as &$row) {
// if (isset($row['content']['#node'])) {
// $node = $row['content']['#node'];
// if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
// foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
// $label = $term->label();
// // Supprimer les accents
// $label_ascii = $transliterator->transliterate($label);
// // Nettoyer pour que ce soit une classe CSS valide
// $class = 'type-' . Html::cleanCssIdentifier(strtolower($label_ascii));
// $row['attributes']->addClass($class);
// }
// }
// }
// }
// }
function eql_preprocess_views_view_unformatted(array &$variables) {
$view = $variables['view'];
if ($view->id() === 'base_de_donnees') {
$transliterator = \Drupal::service('transliteration');
$allowed_types = [
'type-paroles-de-laureats',
'type-publication-issue-du-programme-eqld',
'type-presse',
'type-projets-en-images',
];
$slides = [];
foreach ($allowed_types as $type) {
$slides[$type] = [
'label' => ucwords(str_replace(['type-', '-', '---'], ['', ' ', ' / '], $type)),
'rows' => [],
];
}
$unclassified_rows = [];
foreach ($variables['rows'] as &$row) {
$matched = FALSE;
if (!isset($row['content']['#node'])) {
$unclassified_rows[] = $row;
continue;
}
$node = $row['content']['#node'];
if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
foreach ($node->get('field_type_de_ressource')->referencedEntities() as $term) {
$label = $term->label();
// Supprimer les accents
$label_ascii = $transliterator->transliterate($label);
$type_class = 'type-' . Html::cleanCssIdentifier(strtolower($label_ascii));
// Nettoyer pour que ce soit une classe CSS valide
$class = 'type-' . Html::cleanCssIdentifier(strtolower($label_ascii));
// ✅ Ajoute la classe sur le conteneur .views-row
if (!empty($row['attributes'])) {
$row['attributes']->addClass($type_class);
}
$row['attributes']->addClass($class);
// ✅ Ajoute la classe aussi sur l'article (si tu veux)
// Sassurer que larticle a un attribut HTML
if (!isset($row['content']['#attributes']) || !($row['content']['#attributes'] instanceof Attribute)) {
$row['content']['#attributes'] = new Attribute();
}
$row['content']['#attributes']->addClass($type_class);
if (isset($slides[$type_class])) {
$slides[$type_class]['rows'][] = $row;
$matched = TRUE;
}
}
}
if (!$matched) {
$unclassified_rows[] = $row;
}
}
$variables['grouped_rows'] = array_filter($slides, fn($s) => !empty($s['rows']));
$variables['unclassified_rows'] = $unclassified_rows;
$variables['rows'] = [];
}
}