View filtering all records with no argument, empty text with invalid argument I have a view with a block display that I need to filter by:


All records when no argument present e.g. /foo
All nodes with specified valid taxonomy term ID e.g. /foo/bar
Show empty text when taxonomy term ID is attached to no content e.g. /foo/baz





WHEN THE FILTER VALUE IS NOT AVAILABLE is set to Display all results for the specified field.
WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED is set to Specify validation criteria, Taxonomy Term.
Filter value type is set to term name converted to term ID.
Transform dashes in URL to spaces in term name filter values.
Action to take if filter value does not validate is set to Display contents of "No results found".





The view preview is working correctly, adding taxonomy_term_data_field_data_field_VOCAB.tid = 'xx' to the query but on the front-end it doesn't appear to be working at all.
I have Global: Null as the first entry under Contextual filters as the argument needed to filter is the second argument in the URL string.
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-&gt;argument = $term-&gt;tid;
    return TRUE;
  }
  else {
    return FALSE;
  }
}
else {
  $handler-&gt;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(&amp;$view, &amp;$display_id, &amp;$args) {
  if ($view-&gt;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.