views resources

This commit is contained in:
2025-05-22 13:50:45 +02:00
parent 25d6d72b2e
commit 1bcaf05da4
7 changed files with 227 additions and 346 deletions
+44 -8
View File
@@ -8,6 +8,8 @@
// use Drupal\Core\Link;
// use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\Component\Transliteration\TransliterationInterface;
function eql_preprocess_html(&$variables) {
$node = \Drupal::routeMatch()->getParameter('node');
@@ -29,14 +31,21 @@ function eql_preprocess_node(&$variables){
$type_protagoniste = $node->get('field_type_de_protagoniste')->getString();
$variables['attributes']['class'][] = 'type-de-protagoniste-' . $type_protagoniste;
}
// Vérifie si le champ field_type_de_ressource existe et ajoute une classe basée sur sa valeur
if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
// Récupère la valeur de field_type_de_ressource
$type_de_ressource_entity = $node->get('field_type_de_ressource')->entity;
if ($type_de_ressource_entity) {
$type_de_ressource_name = $type_de_ressource_entity->getName();
// Ajoute la classe CSS basée sur la valeur du type de ressource
$variables['attributes']['class'][] = 'type-de-ressource-' . Html::cleanCssIdentifier($type_de_ressource_name);
// if ($node->hasField('field_type_de_ressource') && !$node->get('field_type_de_ressource')->isEmpty()) {
// // Récupère la valeur de field_type_de_ressource
// $type_de_ressource_entity = $node->get('field_type_de_ressource')->entity;
// if ($type_de_ressource_entity) {
// $type_de_ressource_name = $type_de_ressource_entity->getName();
// // Ajoute la classe CSS basée sur la valeur du type de ressource
// $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;
}
}
}
@@ -94,3 +103,30 @@ 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'])) {
$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);
}
}
}
}
}