1

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.
4
  • Is this Drupal 6? Commented Aug 18, 2015 at 6:24
  • No, this is for Drupal 7 Commented Aug 18, 2015 at 12:45
  • What does the path look like? (The one that isn't working.) Commented Aug 18, 2015 at 13:38
  • See first bullet points. Thx Commented Aug 18, 2015 at 14:41

1 Answer 1

1

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.

7
  • Thanks but I need to show all records when no argument present e.g. /foo Commented Aug 18, 2015 at 15:19
  • WHEN THE FILTER VALUE IS NOT AVAILABLE is set to Display all results for the specified field. Commented Aug 18, 2015 at 15:19
  • And, I need to show the content of "empty text" when term isn't attached to any content. Action to take if filter value does not validate is set to Display contents of "No results found". Commented Aug 18, 2015 at 15:20
  • Partially updated my answer to show value of "No results found". Looking into the other. Commented Aug 18, 2015 at 15:34
  • Interesting exercise, as I didn't think this would be possible without the luxury of AND/OR on contextual filters. Answer has been updated. Commented Aug 18, 2015 at 16:35

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.