1

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

2

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

  • 1
    Or multiple selections: www.mysite.com/news?Category=Chemistry,Astrophysics – manafire Oct 8 at 14:39
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.

  • 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. – usernameabc May 2 '18 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. – usernameabc May 2 '18 at 20:00
  • However, it is now using exposed and contextual filter for the category field. – usernameabc May 2 '18 at 20:06
  • If it works, it works! – sonfd May 2 '18 at 20:26
  • @sonfg is that a "if ain't broke, don't fix it" type of situation now? If having both be exposed and as a contextual filter causing an issue from best practice standpoint, we are open to changing it. – usernameabc May 2 '18 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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