Contextual Filters (known in previous versions of Views as "Arguments") are input. While they often come from the URL, they don't always. Don't be shocked when they don't. Each display type may have its own source for contextual filters. Block displays have no source of contextual filters at all; they cannot pull contextual filters from the URL, and often require use of PHP code as a default contextual filter. Various default values can be applied against contextual filters to use filters in a block view. See "Provide default value," under "When the filter value is NOT in the URL" below.
In general, contextual filters are used to filter the view, and in that sense they have a very close relationship to filters. However, this isn't necessarily true for every contextual filter. Contextual filters can be used for any purpose, really; the extent of what the contextual filter does is up to the developer of the contextual filter.
A typical use of an contextual filter might be to reduce a view to a single node, a single user, or nodes with a given taxonomy term.
When the filter value is NOT in the URL
- Display all results for the specified field
- If this option is selected, the contextual filter is removed from the view as though it weren't there and all results will be displayed.
- Provide default value
- If no contextual filter is given, a default contextual filter can be selected. You may choose from a number of different default filter options. See "Default contextual filters" below.
- Hide view
- The view will remove itself entirely if the contextual filter is not present; for a block this means it simply won't appear. For page views the view will return a 404 and present a "Page not found" error.
- Display a summary
- The view will attempt to display a summary of contextual filters available, based upon the view, and provide links to the view with those contextual filters. Summaries have their own style handlers as well as options. The default summary style simply displays a list of contextual filters with a count of entries found per contextual filter. This special mode is a very powerful part of Views.
- Display contents of "No results found"
- If this option is selected, the value specified under "No results behavior" on the main view page will be displayed when there is no filter value in the URL.
An exception value can be set under the "Exceptions" menu. If this exception value is received, any filter value specified under "When the filter value is NOT in the URL" will be ignored. This is a literal value: if you enter the word "everything" here, the exception will apply only if the value "everything" is received.
Default contextual filters
The Default contextual filter selection is available only if the action to take is "Provide default value" under "When the filter value is NOT in the URL." When that option is selected, a new fieldset will appear that will allow you to choose default contextual filters. Views provides the following default selectors by default (but other modules may add more):
- Content ID from URL
- This will attempt to find a Node ID from the URL, and is primarily useful for the Node: ID contextual filter (though other contextual filters that use Node IDs, such as the CCK Node Reference contextual filter, will find it useful, too). For example, when visiting the path "node/1" or "node/1/edit" it will know that the "1" in that path is a node ID and use it.
- Fixed value
- You may directly enter what the contextual filter will be. This is not a variable, it will always be the same contextual filter.
- PHP Code
- Arbitrary php code inserted here will run whenever a contextual filter is not present in the URL.
- Taxonomy term ID from URL
- This default filter will attempt to return a taxonomy term from the current path. Selecting this option gives you the choice to also load default filters from terms.
- User ID from URL
- Like Node ID and Taxonomy term ID from URL, this will attempt to find a user ID from the path. It also includes an option to look for a node and use the node author as the user ID.
- User ID from logged in user
- If a user is currently logged in and accessing this view, their user ID will be returned as the contextual filter.
- Current date
- This option simply returns today's date.
- Current node's creation time
- Select this to return a node's creation time as a contextual filter.
- Current node's update time
- Not surprisingly, this filter returns the current node's update time
Please bear in mind that the selection of a default contextual filter happens only if a contextual filter is not provided. If you are using a display that has no contextual filter source, such as a block, the contextual filter value selected here will always be used. However, if using a display that reads contextual filters from the URL, the options selected here will only be applied if the URL did not contain an contextual filter.
The "Skip default argument for view URL" option gives you the choice of ignoring the default argument totally. This is useful for certain applications, like the creation of feeds.
When the filter value IS in the URL or a default is provided
- Override title
- Selecting this option will allow you to replace the default view title with a piece of arbitrary text. Argument substitutions in the form of %1, %2, etc. may be used here.
- Override breadcrumb
- This option will allow you to overwrite the view name in breadcrumbs. The same substitution values as in "Override title" may be used.
- Specify validation criteria
- Contextual filters can have validators, which are pluggable systems used to ensure that contextual filters fit within certain parameters. When a validator is chosen, it may provide some specific settings, including the action to take if an contextual filter is presented, but it fails to validate. These actions are generally the same as the default actions above, excluding the ability to provide another default filter. See "Contextual filter validation" below for a detailed description.
Contextual filter validation
Note: If a view provides a menu option, such as a tab, the tab will not appear if the contextual filter does not validate.
This sytem can have other validators plugged in; by default, Views provides:
- Basic validation
- This validator ensures that the contextual filter is present. A PHP NULL value (from eg. PHP default contextual filter code) will invalidate.
- Content
- This validator ensures that the contextual filter is a valid Node ID. You may select which node types the validator will accept.
- Numeric
- This validator ensures that the contextual filter is a valid number.
- PHP Code
- You may enter arbitrary PHP code here, similar to the "PHP code" option under "Provide default value" in "When the filter value is NOT in the URL" above, to determine if the contextual filter is valid or not.
- Taxonomy term
- Ensures that the contextual filter is a valid taxonomy term. This includes options to limit to specific vocabularies and can transform the contextual filter to the right type depending upon the actual contextual filter. Set the contextual filter value type option to the actual type of data that the contextual filter being used is expecting.
- User
- Ensures that the contextual filter refers to a valid user. If you select this validator, additional options are available to accept numeric user IDs, usernames or both, as well as to consider a user's role when filtering the view.
If you select the "Specify validation criteria" option to select a specific validator, you will have the option to select an action to take if the filter value does not validate.
- Display all results for the specified field
- Selecting this option will return all values if the filter value does not validate, similar to "Display all results for the specified field" under "When the filter value is not in the URL" above.
- Hide View
- Similar to "Hide view" under "When the filter value is NOT in the URL" above, if this option is selected and the selector does not validate, the view will hide itself. If the view is a block, nothing will appear. If it is a page, it will throw a 404 and present a "Page not found" error.
- Return Summary
- If you select this option and the filter does not validate, a summary of all values that are valid will be returned, as in the option of the same name under "When the filter value is NOT in the URL above."
- Display contents of "No results found"
- If this option is selected and the selector does not validate, the value specified under "No results behavior" on the main view page will be displayed.
Adminstrative title
Located under the "More" group at the bottom of the "Contextual filters" menu, this option allows you to specify a custom name for the contextual filter. This may be particularly useful if you use the same contextual filter twice and you'd like to distinguish between the two filters.
Grouping of contextual filters
Even though contextual filters do not appear in the "and/or" user interface for sorting and grouping regular filters, contextual filters are always added to the first group of filters. Thus the order of the groups can cause the contextual filter to have entirely different effects on the results of a view that has contextual filters. Even though differences might not be apparent through the user interface.
Multiple contextual filters are therefore always in the same "and/or" group of filters, and can not be placed in different groups. There is an effort to add this feature.