1

I want to create a view that will allow the user to pass a filter value using the URL, but if the filter is missing, assign a default value. The view's path is set to results. I created a view page and added a contextual filter that uses the URL to retrieve the value results/somevalue. Some value will filter the view based on it's value. What I can't figure out is if the user browses to the default page, results, how do I set a default value?

I tried creating a second page based on the first, using the same path. When I did this, users could not browse to results/somevalue.

3 Answers 3

2

I would suggest a small trick with contextual filters.

After adding a filter:

  1. You could simply select and fill the 'Provide default value' field.
    Here you have many options to mention the default value. The one which should work for you is: "Raw value from URL"

Alternative:

  1. Set 'WHEN THE FILTER VALUE IS NOT AVAILABLE' to (Display contents of "No results found")
  2. Select 'Allow multiple values' option at the bottom (by clicking on more)
  3. Now you should pass two filter with the URL

    1. Your default filter
    2. Filter which user will add to url.
  4. So your default url should always include the default parameter (filter).
  5. Once this is done, when there is no value for the user's filter view should filter the results based on the default filter.
1

This works well and is, in my mind, about as straightforward as you get for this question:

  1. Make your View (let's call it View A)
  2. Select things to show (fields, etc)
  3. Create a Contextual Filter and Provide default value with type "Content ID from URL" (or some other contextual filter mechanism)
  4. Clone the View to make View B
  5. Remove the Contextual Filter from View B and add a regular Filter (Filter criteria) to be your default argument
  6. Go back to View A
  7. Under "No Results Behavior" add "Global: View area" and choose View B (listed by machine names)
  8. Hit Save

Voila!

0

Another solution is to use the module 'Views Contextual Filters OR' which allows you to enable OR logic in views arguments instead of the default AND logic. This means that after enabling OR in the Query settings of the view, you can add an extra views contextual filter and use Provide default value > Fixed value.

The Fixed value filter needs to be placed at the bottom of the contextual filter section. This is because the OR logic uses the first filter that returns anything, disregarding the rest. It works from top to bottom.

NOTE: of your view uses multiple contextual filters which rely on the default AND logic, this solution won't work because this module converts everything to OR. In that case you might want to use another module: Views Evi

Views EVI allow boths AND/OR logic filtering in contextual filters, the same way as normal views filters can do by reordering and dragging dropping.

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.