4

Short version, general question: You want to pipe in a 'Default value', specifying a URL argument, to a contextual filter in Views 3. You also want all results (the full unfiltered view) to be displayed if this argument in the URL is empty. However, "Default value" and "Display all rows if empty" settings in Views contextual filters are mutually exclusive, and the default if nothing is passed to the filter is to show nothing.

How can you achieve this?


Full version with case specifics: I'm using a Context View with a contextual filter on a Search API index to search based on search terms in a URL: path like "/search/my%20search%20terms, loaded into a Page Manager panels page as a page context so that bits of the view can be dispersed across the page regions. Pretty standard use of Search API Views.

Since you can't specify what arguments or contexts to pass to a context view in Page Manager / Panels when loaded as contexts in the Contexts form (unlike block views etc, loaded as content, where you can explicitly pass in arguments or contexts), I need to tell it which URL argument it will find the search terms in using the 'Provide default' options under Contextual Filters in the Views settings for the view.

This works, but has an unintended side effect - if someone goes to the search page without any terms (navigates to /search or submits an empty search box), they get nothing. I'd like this path to show the full unfiltered view (it's full of search facets and such so just navigating to it without search terms works like a drill-down browse/advanced search page).

If this was a block or loaded as content in panels/page manager, I could set "If no filter value is provided" to show all rows and use panels/page manager to pass in the current URL argument. But, in Views UI, default values and 'Show all' are mutually exclusive.

So, how to use a default value for a contextual filter, then show all if that default value isn't present or is empty?

2 Answers 2

5

Even faster: PHP is not even needed to just display all values...

  • Under "When the filter value IS available or a default is provided
  • Tick "Specify validation criteria" Choose 'Basic validation'
  • Under "Action to take if filter value does not validate", choose "Display all values"
2

Here's something that works, not sure if it's the best way (can only be done by admins with Views UI PHP priviledges):

  • Under "When the filter value IS available or a default is provided" tick "Specify validation criteria"
  • Choose PHP code and enter: return empty($argument) ? false : true;
  • Under "Action to take if filter value does not validate", choose "Display all values"

So, it displays everything if it gets an empty ('' or null) default value.

It works fine, but feels like a cludge - I might be missing some way to pass contexts/args in page manager or some simpler way to achieve this in Views.

1
  • Excellent, thanks for sharing this. In my case, I needed a placeholder value for a date (yyyy-mm) argument. Just a little PHP did the trick: if ($argument == 'current') { $handler->argument = date('Y-m'); } return TRUE; Now, when I use the word 'current' as the first argument, it will default to the current month and views will accept all additional arguments. Commented Mar 1, 2013 at 21:13

Your Answer

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

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