4

We have a View which has the following settings. How can we setup the view to use a query parameter from the URL to filter the results?

Article Content Type (fields):

  1. Title
  2. Body
  3. Category (field_category) --> taxonomy
  4. Type (field_type) --> taxonomy

View Exposed Filters (values)

  1. Category (All, Security, IoT, Wifi, Network)
  2. Type (All, Blog, Press Release, Breaking News)

Desired output:

  • When the URL is www.mysite.com/news?type=Blog, then set filters Type=Blog and Category=All to return results of all Article pages where type=Blog and Category can be any (not null)
  • When the URL is www.mysite.com/news?type=Blog&Category=Security then filter results to return all Article pages where type=Blog and Category=Security

We tried adding a contextual filter with the following settings only but it did not work. context fitlers

0

2 Answers 2

3

Views Exposed filters already work this way, no need for any Contextual filters.

In the settings for each Exposed filter there is an option to type in Filter identifier. That is the text that will be used for that field in the query.

In your case set the Filter identifier of the Category to Category and of Type field to type.

exposed_Category

If you configure the Exposed filter Selection type settings for those two filters to Dropdown then you'll have to use term IDs in the link to make the filter work (I'm assuming your Security term has ID 6):

www.mysite.com/news?Category=6

Use Autocomplete to get term names in query

But if you set the Exposed filter Selection type settings to Autocomplete you will be able to use term names, which is what you want.

www.mysite.com/news?Category=Security

or

www.mysite.com/news?Category=Security+(6)

Drupal 8 adds the term ID next to the term name when choosing from the autocomplete suggestion list, but that part isn't necessary. You can construct the link with Category name only and it will work. If the Category name has spaces in it, replace them with + in query.

If you have more than one Exposed filter they get combined, for all types:

www.mysite.com/news?Category=Chemistry

or

www.mysite.com/news?Category=Chemistry&type=

and for some specific type in addition to Category:

www.mysite.com/news?Category=Chemistry&type=Blog

or change order:

www.mysite.com/news?type=Blog&Category=Chemistry

6
  • 1
    Or multiple selections: www.mysite.com/news?Category=Chemistry,Astrophysics
    – manafire
    Oct 8, 2019 at 14:39
  • Thanks this was helpful. I was searching for where to add this option.
    – Binny
    Nov 6, 2020 at 3:19
  • I can attest that while this might work with a radio/checkbox option, it does not work if you are using Better Exposed Filters and are trying to show the default dropdown. This is because that throws an error. Fun times!
    – Patrick
    Aug 3, 2021 at 20:03
  • Specifically, setting the exposed filter to autocomplete throws errors when using BEF.
    – Patrick
    Aug 3, 2021 at 20:09
  • Good to know about the filter identifier. Is there a simple way to do the reverse, i.e. update the query parameters based on the filter selection. Not even sure if this is a good idea but would be helpful for linked to search results, (I have also seen this pattern abused with third parties skewing analytics reporting with links to their porn sites). As you say you can share link to if you know the correct pattern. How best to update the browser url?
    – Daniel
    Jul 31 at 20:56
0

When using the labels of entities in filters, instead of the id, I need to "Specify validation criteria" and select the appropriate option. In this case I would "Taxonomy term name" validator. These options appear in the "WHEN THE FILTER VALUE IS IN THE URL OR A DEFAULT IS PROVIDED" group.

Update: I didn't realize you were using Exposed and Contextual filters when I originally posted -- I thought you were just using Contextual. Also, I do think it's not a "best practice" to use contextual and exposed filters for the same filtering, but I think it's fine (if you're seeing the behavior you want) because I don't know of a way to use just Exposed Filters and get the label in the url like you want. I'm bet there's a way using custom code, but I don't know it.

7
  • It is updating the exposed filter value to be the one selected, but the results are not being return. You gave me an idea, i will update when available. May 2, 2018 at 19:41
  • I changed "When the filter value is not available" to use Display all results for the specified field and made the setting changes you recommended and it worked. May 2, 2018 at 20:00
  • However, it is now using exposed and contextual filter for the category field. May 2, 2018 at 20:06
  • If it works, it works!
    – sonfd
    May 2, 2018 at 20:26
  • 1
    I was fully aware that we were using exposed and contextual filter which is why i posted it as "We tried adding a contextual filter with the following settings only but it did not work.". I have not found any documentation or discussion indicating what is best practice between the uses of both. May 3, 2018 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.