28

In D7 I was able to filter a view according to a taxonomy term name taken from the URL. An example would be this URL

http://www.gbyte.co/tags/drupal-planet/rss.xml

"drupal-planet" would be converted to the taxonomy term ID which then would filter the view results accordingly.

Now I have set up a corresponding D8 view with a contextual filter "Content: Tags (field_tags)", field_tags being the taxonomy field. However the D8 view will only show results when I pass the actual term ID into the URL like this:

http://www.gbyte.co/tags/9/rss.xml

I would like it to also work with the term name like D7 did. Is the functionality missing, or do I need to configure the view differently?

1
  • Any news on this? I have the same problem
    – jeroen
    Commented Dec 23, 2015 at 21:55

8 Answers 8

33

The Term name converted to Term ID filter looks to be missing for D8 Views.

One site-builder workaround, is to add a Relationship, Taxonomy terms on node then to add a Taxonomy Term: Name filter to Contextual Filters using the aforementioned relationship. Checking the Specify validation criteria checkbox, set your Validator to Taxonomy term name & check the appropriate Taxonomy to filter on.

2
  • 15
    Site-builder pitfall in the D8 add contextual filter UI: the title of the contextual filter is 'Name', and the category is 'Taxonomy term'. Searching for 'taxonomy' will fail to list this filter, since the title and description only are searched, not the category.
    – keithm
    Commented Aug 24, 2016 at 18:39
  • 2
    Additionally you can overwrite the title with {{ arguments.name }} in the settings of the contextual filter. Commented Mar 21, 2017 at 17:21
8

To expand on Shawn Conn's answer:

  • Within the view config, under Advanced -> Relationships click 'Add'
  • Now select 'Taxonomy terms on node'
  • On the next screen choose the 'tags' vocabulary and require the relationship.
  • Now under Advanced -> Contextual Filters click 'Add'
  • Under 'When Filter is NOT available', choose 'Provide default value', 'Raw value from URL'. Select any other options appropriate to your needs.
  • Under 'When Filter IS available or a default value is provided' choose 'Specify Validation Criteria' and select 'Taxonomy Term Name' for the validator. Choose the 'tags' vocabulary and then click apply.

Hope that helps!

1
  • 2
    The fourth bullet point is missing what you're supposed to add. It should be "Name" (Taxonomy term).
    – sjhuskey
    Commented Jul 27, 2022 at 15:14
4

Just for the sake of completion, at the time of writing the question I've managed to get around this issue by converting the taxonomy term name from the URL to a term ID in views_hook_pre_view() and then using it as input for the contextual filter which accepts taxonomy IDs.

Here is the code for the views hook:

/**
 * Implements hook_views_pre_view().
 */
function my_module_views_pre_view(\Drupal\views\ViewExecutable $view, $display_id, array &$args) {
  if ($view->id() == 'blog' && $display_id == 'feed') {
    if (isset($args[0])) {
      $term = taxonomy_term_load_multiple_by_name($args[0], 'tags');
      if (empty($term)) {
        $term = taxonomy_term_load_multiple_by_name(str_replace("-", " ", $args[0]), 'tags');
      }
      if (!empty($term)) {
        $tid = key($term);
        $args[0] = $tid;
      }
    }
  }
}
3

@kiethm Has provided the correct way to approach this - Under the Contextual Filters you need to select 'Name' and not 'Taxonomy Term ID' then set your validation criteria to use 'Taxonomy Term Name'.

2

I don't know if you still have this problem but I was able to create a relationship for "Taxonomy terms on node" using the desired taxonomy vocabulary and then create a contextual filter using that relationship for "Taxonomy term: Name" which allows you to validate based on name rather than TID.

1
  • 2
    I have the same problem described in this question, can you be more clear. I already set a relationship but contextual filter only works with tid instead term name. Of course we are talking about D8. Thanks in advance.
    – Vagner
    Commented Apr 1, 2016 at 20:31
2

I wrote a tiny module to restore the missing functionality and get this working properly again:

https://www.drupal.org/project/views_taxonomy_term_name_into_id

It re-adds the 'Convert term name into ID' argument validator for taxonomy terms used in 'contextual filters' in Views. See the project page for more info and instructions.

Enjoy, -Derek (dww)

1

Hey I have been looking for this answer for a while. For anyone else like me looking for how to make it work, I Gumped my way into my into success partly from here and drupal.org's results and then also from following: http://redcrackle.com/blog/adding-contextual-filter-view-drupal-8

I'm using Drupal 8 and Zircon. I'm brand new so I may use the wrong terms. I wanted to make contextual image links in "Panel first col 1" based on the Category of the page that someone is on. What I did was go to Structure > Views > Carousel (the view that has the pictures in that block region) > Advanced > Add by Contextual Filters > Has taxonomy term ID > select in Provide default value: Taxonomy term ID from URL (which made no sense to me but whatever) > then I checked all three boxes: Load default filter from term page, Load default filter from node page, that's good for related taxonomy blocks, Limit terms by vocabulary. Then for Vocabularies I checked Categories.

PHEW! Finally! Thanks to you all on here for pointing me in the right direction! I could not explain why it works, totally, but whatever, it does and I will keep being employed.

0

I have achieved this in Drupal-8 by taking contextual filter: of "Taxonomy term: Name"

Then Provide default value Type Taxonomy Term ID From Url

Then specifying the following validation criteria:

enter image description here

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.