Skip to content

Filtres exposés

This content is not available in your language yet.

Un filtre exposé est un filtre de vue que l’utilisateur peut modifier. Il génère un formulaire de recherche/filtrage.

TypeWidgetUsage
SélectionSelect / CheckboxesCatégories, marques
TexteChamp texteRecherche par mot-clé
NumériqueMin/MaxFourchette de prix
DateSélecteur de datePériode
BooléenCheckboxEn stock / Promo

Nous allons ajouter :

  1. Filtre par catégorie
  2. Filtre par marque
  3. Filtre par fourchette de prix
  4. Recherche par mot-clé
  5. Tri exposé

Ouvrez la vue products_catalog :

  • StructureViewsCatalogue ProduitsEdit
  1. Dans Filter Criteria, cliquez sur Add

  2. Recherchez “category” et sélectionnez Content: Category (field_category)

  3. Cliquez sur Add and configure filter criteria

  4. Configurez :

ParamètreValeur
Expose this filter to visitors☑ Oui
Required☐ Non
LabelCatégorie
OperatorIs one of
VocabularyCatégorie Produit
Selection typeDropdown
  1. Dans More :
ParamètreValeur
Filter identifiercategory
Remember last selection☐ Non
  1. Apply

Répétez le processus :

  1. Add → Rechercher “brand” → Content: Brand (field_brand)
ParamètreValeur
Expose this filter☑ Oui
LabelMarque
OperatorIs one of
VocabularyMarque
Selection typeDropdown
Filter identifierbrand

Pour le prix, nous avons besoin de deux filtres : Min et Max.

  1. Add → Rechercher “price” → Content: Price (field_price)
ParamètreValeur
Expose this filter☑ Oui
LabelPrix min
OperatorIs greater than or equal to
Value(vide)
Filter identifierprice_min
  1. AddContent: Price (field_price) (nouveau)
ParamètreValeur
Expose this filter☑ Oui
LabelPrix max
OperatorIs less than or equal to
Value(vide)
Filter identifierprice_max
  1. Add → Rechercher “title” → Content: Title
ParamètreValeur
Expose this filter☑ Oui
LabelRechercher
OperatorContains
Case sensitiveNon
Filter identifiersearch
  1. Dans Sort Criteria, cliquez sur le sort existant Content: Authored on

  2. Cochez Expose this sort to visitors

  3. Apply

  4. Ajoutez d’autres critères de tri :

AddContent: Title

  • Label : Nom (A-Z)
  • Order : Asc
  • Expose : Oui

AddContent: Price (field_price)

  • Label : Prix
  • Order : Asc
  • Expose : Oui
  1. Configurez les labels du tri :

Dans Sort Criteria, cliquez sur Settings (engrenage) :

ParamètreValeur
Exposed sort labelTrier par
  1. Dans la colonne Advanced (à droite)
  2. Cliquez sur Exposed formSettings
ParamètreValeur
Exposed form styleBasic
Submit button textFiltrer
Reset button☑ Oui
Reset button labelRéinitialiser
Exposed sorts labelTrier par
Sort asc labelCroissant
Sort desc labelDécroissant
  1. Apply

Par défaut, le formulaire est dans le header de la vue.

Pour le déplacer dans un bloc séparé (sidebar) :

  1. Exposed formExposed form in block : ☑ Oui
  2. Sauvegardez la vue
  3. Placez le bloc dans la région souhaitée via Block layout

Pour un formulaire plus moderne, installez le module Better Exposed Filters :

Fenêtre de terminal
composer require drupal/better_exposed_filters
drush en better_exposed_filters -y
  1. Retournez dans la vue
  2. Exposed formExposed form style → Sélectionnez Better Exposed Filters
  3. Apply
  4. Cliquez sur Settings pour configurer :
FiltreWidget BEF
CategoryCheckboxes/Radio
BrandCheckboxes/Radio
Price min/maxSlider (avec JS)
SearchAutocomplete
SortLinks

Les filtres modifient l’URL avec des paramètres GET :

/shop?category=5&brand=2&price_min=20&price_max=100&search=nike&sort_by=field_price&sort_order=ASC

Cela permet :

  • Partager un lien filtré
  • Bookmark d’une recherche
  • SEO (pages catégorie)

Pour créer des pages comme /category/t-shirts ou /brand/nike :

  1. Dans AdvancedContextual Filters, cliquez sur Add
  2. Recherchez “category” → Content: Category (field_category)
  3. Configurez :
ParamètreValeur
When filter value is NOT availableDisplay all results
When filter value IS availableSpecify validation criteria
ValidatorTaxonomy term
Vocabularies☑ Catégorie Produit
Filter value typeTerm name converted to Term ID
Transform dashes☑ Oui
  1. Dans More :
ParamètreValeur
Case in pathLower case
  1. Apply
  1. +AddPage
  2. Display name : Page Catégorie
  3. Path : /category/%
  4. Menu : Normal menu entry → Catégorie

Le % sera remplacé par le terme de taxonomie.

  • /category/t-shirts → Produits catégorie T-shirts
  • /category/chaussures → Produits catégorie Chaussures
  • /brand/nike → Produits marque Nike
  1. Sauvegardez la vue : Save
  2. Visitez /shop
  3. Testez chaque filtre :
    • Sélectionnez une catégorie
    • Sélectionnez une marque
    • Entrez un prix min/max
    • Recherchez un mot
    • Changez le tri

L’URL doit se mettre à jour avec les paramètres :

/shop?category=5&brand=2&price_min=0&price_max=100
Fenêtre de terminal
drush cex -y

Ajoutez du style au formulaire exposé :

/* Formulaire de filtres */
.views-exposed-form {
display: flex;
flex-wrap: wrap;
gap: 1rem;
padding: 1rem;
background: #f8f9fa;
border-radius: 8px;
margin-bottom: 2rem;
}
.views-exposed-form .form-item {
flex: 1;
min-width: 150px;
}
.views-exposed-form .form-item label {
display: block;
font-weight: 600;
margin-bottom: 0.25rem;
}
.views-exposed-form .form-select,
.views-exposed-form .form-text {
width: 100%;
padding: 0.5rem;
border: 1px solid #ddd;
border-radius: 4px;
}
.views-exposed-form .form-actions {
display: flex;
gap: 0.5rem;
align-items: flex-end;
}
.views-exposed-form .button {
padding: 0.5rem 1rem;
background: #0073e6;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.views-exposed-form .button--secondary {
background: #6c757d;
}
  • Filtre catégorie exposé
  • Filtre marque exposé
  • Filtres prix min/max
  • Recherche par mot-clé
  • Tri exposé (date, prix, nom)
  • Boutons Filtrer/Réinitialiser
  • (Optionnel) BEF installé
  • (Optionnel) Page /category/% créée
  • Configuration exportée

Les filtres sont en place ! Créons maintenant les Blocs dynamiques.