All of the methods below will display:
- Matching results on foo/bar (valid term name with matches)
- Value of "No results found" on foo/norf (valid term name with no matches)
- Value of "No results found" on foo/baz (invalid term name) (Page display) or 404 Page Not Found (Block display)**See note below.
The following methods will take different actions if a term is not present; e.g. foo/.
Method 1. Display NO results if term is not present
Contextual filter: Content: Has taxonomy term ID
When the filter value is NOT available .. Provide default value
Type .. Raw value from URL
Path component .. 2
Use path alias .. checked
When the filter value
IS available or a default is provided .. Specify validation criteria
Validator .. Taxonomy term
Vocabularies .. (select vocabularies)
Filter value type .. Term name converted to term ID
Transform dashes in URL to spaces
in term name filter values .. checked
Action to take if filter value does
not validate .. Display contents of "No results found"
Method 2. Display ALL results if term is not present (using PHP filter module)
Note: For this method the core module PHP filter must be enabled.
Contextual filter: Content: Has taxonomy term ID
When the filter value is NOT available .. Provide default value
Type .. Raw value from URL
Path component .. 2
Use path alias .. checked
When the filter value
IS available or a default is provided .. PHP code
PHP validate code .. (see below)
if ($argument) {
$vocabulary = 'vocabulary_name'; // The machine name of the vocabulary.
$terms = taxonomy_get_term_by_name($argument, $vocabulary);
if (!empty($terms)) {
$term = array_shift($terms);
$handler->argument = $term->tid;
return TRUE;
}
else {
return FALSE;
}
}
else {
$handler->argument = 'all';
return TRUE;
}
Action to take if filter value does
not validate .. Display contents of "No results found"
Method 3. Display ALL results if term is not present (using Views hook)
Contextual filter: Content: Has taxonomy term ID
When the filter value is NOT available .. Provide default value
Type .. Raw value from URL
Path component .. 2
Use path alias .. checked
When the filter value
IS available or a default is provided .. Specify validation criteria
Validator .. Taxonomy term
Vocabularies .. [select]
Filter value type .. Term name converted to Term ID
Action to take if filter value does
not validate .. Display contents of "No results found"
Add this function to a custom module and clear the caches:
function MODULENAME_views_pre_view(&$view, &$display_id, &$args) {
if ($view->name == 'view_name') { // You can optionally also check the display ID.
if (!empty($args)) {
$argument = $args[0];
$vocabulary = 'vocabulary_name'; // The machine name of the vocabulary.
$terms = taxonomy_get_term_by_name($argument, $vocabulary);
if (empty($terms)) {
$args[0] = FALSE;
}
}
else {
$args[0] = 'all';
}
}
}
Method 4. Display ALL results if term is not present (no coding required!)
On your first block display, use the contextual filter in Method 1, then limit the block visibility*** to 'foo/*'. Create a new block display that outputs all results and limit the block visibility to 'foo'.
**There's an important caveat when using URL arguments with a block display: the URL must be a valid path. In other words, foo/bar must be an actual page or you will get a 404.
*** In the Pages fieldset on the block configuration form (Administration > Structure > Blocks), select "Only the listed pages", then enter the suggested values.