Views contextual filter on taxonomy term ID, with - override title - only has replacement patterns for term ID available:

{{ arguments.field_taxo_term_prod_category_target_id }}
{{ raw_arguments.field_taxo_term_prod_category_target_id }}

Views should provide replacement token(s) that yield the TERM NAME, not the term ID (which is essentially useless).

If there is some other way to achieve the appropriate result, to display the - term name of the current taxonomy term when a View traverses a taxonomy vocabulary, what is it?

===
step to reproduce

Create a view page with /taxonomy/term/% path and a contextual filter on an associated taxonomy term.

The option for "override title" and "replacement patterns" only provides the "target id", and not the term name, when it is the taxonomy term name that is surely the 99.99% desired use case.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DrupalSiteBuilder created an issue. See original summary.

bertonha’s picture

Not sure if I understood your issue, but Drupal allow you to use the replacement pattern {{ name }} directly in the Title field of your view:

view

see:

2

websiteworkspace’s picture

You have not understood the problem ...

The problem occurs with "contextual filters" which are available via the "advanced" section at the right side of Views configuration. Your screenshot does not even have "advanced" Views features opened. In your screenshot the "advanced" settings at the right are closed.

This is a problem with "contextual filters" and the "override title" feature within the "contextual filters" settings, when building a taxonomy term traversal view.

Examine the following screenshot carefully.

The View setup below is a special type of view called a "taxonomy traversal" view. It is a page type view. The path of this page type view is /taxonomy/term/%, wherein the % is the ID of the current taxonomy term in the URL. (parenthetical) Take note that all the taxonomy term URLs are aliased with the pathauto module - ([term:vocabulary]/[term:parents:join-path]/[term:name]). However, that should not matter because the taxonomy term has a name anyway.

Look carefully at the right side of the screenshot below at the right column, the advanced column. The advanced settings includes a "contextual filter" called "Content: has taxonomy term ID (with depth)".

view

When the "Content: has taxonomy term ID (with depth)" settings are displayed, it looks like the following:

contextual filter

In the screenshot above take note of two imiportant features:

1 - the checkbox that says "override title"
- and the edit field just below

and

2 - the section "replacement patterns", which are:

{{ arguments.term_node_tid_depth }} == Content: Has taxonomy term ID (with depth) title
{{ raw_arguments.term_node_tid_depth }} == Content: Has taxonomy term ID (with depth) input

--

In the 99.9% use case, a site builder will want to display the term NAME not some number

For example the override title field content might be:

Articles with Tag {{ arguments.term_node_tid_depth }}

--

Where the output of the title when the view is displaying article nodes that match the taxonomy term would be the

Articles with Tag Foo

However, the available replacement patterns only produce

Articles with Tag 21

Where the 21 is the term ID of the taxonomy term.

===============

This problem does not exist in Drupal 7 Views. Drupal 7 Views behave correctly and display the term name. However, Drupal 7 views use a different syntax method altogether (beyond scope of those bug report).

==============
PRIMARY DIFFICULTY AND SOURCE OF PRIMARY PROBLEM

The replacement patterns just described need to provide replacement pattern options that produce the taxonomy term NAME when using the "override title" option NOT, the taxonomy term ID.

If Drupal 8 Views doesn't currently implement this feature it needs to. It is absolutely astonishing that this problem hasn't been reported as a bug by now since this is a very important Views feature. Without this feature, the style of View shown above is next to impossible to implement without a lot of backend customization. This type of View with the "override title" feature ought to be a snap to setup for site builders. It is the 99.999999% use case.

bertonha’s picture

Yep
My view DO have a contextual filter, but it is not expanded.
And yes, I understood your point. What I am saying is that you don't need to use this override title behaviur inside contextual filters. If you just use the tag directly you can have the result that you want to achieve.
If you pass a different term id as a contextual filter parameter, the {{ name }} will change according the taxonomy id.

t

websiteworkspace’s picture

What you are doing is very different!

The view you have created is very different and does not illustrate the same problem and won't solve the problem I've described.

Look carefully at the example I have provided. My example does NOT have a taxonomy vocabulary at the left side from which to derive the {{ name}} variable!!!

What you are doing is different!

Your view is a view of the vocabulary itself, not a view of content associated with a vocabulary!

Imagine that you want to create a view that displays "cards" from specific fields of content, or even the simplified example above which displays content node teasers, on pages where the attached tags vocabulary reference on the node matches. One of the fields in the nodes references a taxonomy, so it is available for the contextual filter. In my actual site, the taxonomy is a different custom taxonomy, and taxonomy has View specifically attached to it using the taxonomy view integration module (TVI).

Your solution above solves a different problem than the one I've describe. Your solution will not work in the context of the example I've provided. WILL NOT fix the problem illustrated by bug report. Try exactly matching the example I've provided, and you will see that what you have tried doesn't work. Make a Content View page on the article content type using the /taxonomy/term/% path and build from there.

This bug report and thread is about the bug in Views which is the missing Views feature I've describe. This feature used to work in Drupal 7! But in Drupal 8 the same feature does not work correctly, it displays the term ID and not the term name when using "override title" on a context filter when the view is a view of nodes.

This bug report requests that the maintainers of views.module in Drupal 8 core fix the bug I've described, a missing feature that worked in D7, so that it works correctly in Drupal 8.

bertonha’s picture

Ok! Good luck :)

swentel’s picture

Priority: Critical » Normal
FileSize
25.52 KB
11.03 KB

This is not critical per https://www.drupal.org/core/issue-priority

Also, you show a screenshot where your replacement in the title override is different than the actual available replacements, so this is kind of confusing.

Also, this should work, take a look at the drupal 8 default taxonomy view, that is using a replacement pattern and prints out the term name fine (even if the machine name of the replacement is confusing, I agree, but it description explicitely says it will print the title). I tested with depth as well and works fine.

view config

output

Jax’s picture

fyi, I'm having the same issue as @websiteworkspace. Here the contextual filter is on a reference field. For example list the contacts of a company.

A contact has a field company_reference and the contextual filter is on that one.

And while I'm typing this I think of a workaround:

* Add the relation to the target entity.
* Use the ID contextual filter.
* Override title and use the correct replacement pattern. Now the title is shown.

maxdev’s picture

I have the same issue and I figured out, that it reproduces only when you use node's taxonomy term reference field for contextual filter. (In my case field_category).
I replaced it with "Has taxonomy term id" contextual filter and now it shows term's title instead ID.
But I suppose it still a bug, because in D7 it worked even with reference fields.

max84’s picture

Hello,

I think feature could be great. Let's me explain another case.
Content type A has Taxonomy Vocabulary 1
Content type B has Taxonomy Vocabulary 2

Some terms could be into Vocabulary 1 and 2 (but ID are different).
If i want to display with view some related content, for example displaying onto content A page related content from content B, i can't use taxonomy ID. I must use taxonomy name.
It was possible with Drupal 7, but not anymore with Drupal 8.

But maybe i mess something ?

Best regards,

Ace Cooper’s picture

charly71’s picture

+1 ..same problem for me. I want to override views title with contextual filter's label.

dww’s picture

See https://www.drupal.org/project/views_taxonomy_term_name_into_id for a related problem that might actually give many of you what you need. That gives you tokens for both the ID and the label, and generally results in more efficient queries. Good luck.

phjou’s picture

Hi,

I've encountered the same issue. I tried many things and some patches but nothing was working. (and I wanted another field than the term name, so it is even more complicated)

I thought about a simple trick that works great waiting the solution directly inside views. I have just written a Twig Extension to get the data that I want. I pass the term id in the twig function and then I load my term and extract the data I need.
The override title input in the view configuration allows you to use twig, so this workaround should work. I hope it will help some people.

phjou’s picture

I post the example if someone want it. I've written it really quickly, you should use a dependency injection to load you Term and add some checks :)

use Drupal\taxonomy\Entity\Term;

/**
 * Class DefaultService.
 *
 * @package Drupal\demo_module
 */
class TwigExtension extends \Twig_Extension {

  /**
   * {@inheritdoc}
   * This function must return the name of the extension. It must be unique.
   */
  public function getName() {
    return 'tvshows_config';
  }

  /**
   * In this function we can declare the extension function.
   */
  public function getFunctions() {
    return array(
        new \Twig_SimpleFunction('taxonomyFieldFromId', array($this, 'taxonomyFieldFromId'), array('is_safe' => array('html'))),
    );
  }
  /*
   * This function is used to return alt of an image
   * Set image title as alt.
   */
  public function taxonomyFieldFromId($id, $field_name) {
    $field = '';
    $term = Term::load($id);

    if (!empty($term)) {
      $field = $term->{$field_name}->value;
    }

    return $field;
  }

}

portulaca’s picture

There is a way to use term related information in content/node views, it is through Relationships. It was already mentioned previously in this issue but I guess it wasn't clear enough.

Entity reference fields store only the ID of the entity they are referencing. It wouldn't make sense to copy **everything** about the term into the node that is tagged with the term.

Term information is held in its own database table, separate from the place where content/node data is. Views is a tool that builds queries **after** you've chosen the base table to pull info from. When you're building Content Views the content table is used as the base, when you're listing users then the users table is being used as the base, etc.

There is a way to pull related data from other tables into your Views. It is done through Relationships.

If you have Content Views you only have the term ID number available as a Contextual filter, out-of-box that is. But if you add the **Relationship** for the taxonomy reference field you are making all the term data available to this View!

So after you have added the Relationship for the taxonomy term into your Content Views, the **Name** Contextual filter becomes available it's in the Taxonomy term Category. That Category is added and made available on the Contextual filters list after adding the Relationship!

I'm aware that you don't need the Relationship to show the term names in Fields, but that was done through some extra code, Formatter "magic".

In D7 more "magic" was done especially for Taxonomy module in Views so you can do stuff without adding Relationships, but we can view those as extras because you can still build Views to behave the same, it's just a slightly different procedure (add Relationship). I don't know why some parts weren't ported to D8, it probably wasn't straightforward.

And adding special features for Taxonomy module only doesn't help a lot of other similar cases where we're referencing other entities. For example when referencing article Content type nodes from Basic page nodes. To build Views that list related nodes contextually we have to use Relationships, even in D7.

I'd love to have the extra Taxonomy support in Views if it optimizes the query performance or helps in some other way (usability), but otherwise I don't see it as a bug and I'm ok with having to add the Relationship for extra info.

BTW there is a bug related to relying on Taxonomy term Names to filter - if you have a term with the same name in different Vocabularies - vocabulary validation isn't working properly: TermName views argument_validator is not working as expected

cchoe1’s picture

There is also the issue of standard views filters. If you have an entity with a reference to some sort of taxonomy and you expose the filter to visitors, it will still want to reference tags via ID and not name. This issue is more difficult to workout because if you configure a relationship, it will not give you the same options to allow them to select from a dropdown/autocomplete and instead gives a textbox.

In the case of a 'tags' property, you don't want to make the visitor guess as to what tags may exist for a given entity (say, we tag our blog posts of Node entity with descriptive terms like 'educational' or 'personal'). If you want to give your users a dropdown list to choose from, this will cause the tags to insert IDs as url parameters and not the name property.

I'm still looking for a solution to this since we have an e-commerce site with products tagged with taxonomy terms as descriptors. Commerce's custom taxonomy module is quite buggy and results in a lot of weird errors when working with views (issues with parent terms when nesting tags, etc.)

Will probably need to write a custom views filter plugin or something along those lines to get this to work, e.g. when I filter products by something like 'clothes' I want the url parameter to be 'tags=clothes' instead of 'tags=43'.

bserem’s picture

Version: 8.4.0 » 8.8.x-dev

There is a module that does the exact opposite: https://www.drupal.org/project/views_taxonomy_term_name_into_id
Maybe code there will help you.

In any case, what you ask would be nice for drupal core.

UPDATE: if you check into admin/structure/views/view/taxonomy_term you will see how Drupal core does it.

dww’s picture

As maintainer of https://www.drupal.org/project/views_taxonomy_term_name_into_id I should clarify. What it does is add an argument validator plugin for taxonomy term arguments to convert human readable names into term IDs.

So if you have a view like /news/%category

You can use the term ID argument (better queries) but the URLs to access it can be stuff like:

/news/international
/news/environment
...

See the docs on the project page for more.

But this is only for contextual filters (aka "arguments"), not regular exposed filters.

Cheers,
-Derek

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

colan’s picture

djg_tram’s picture

There is a solution but not a terribly pretty one. Include this in your .module file:

function yourmodule_views_pre_render(ViewExecutable $view) {
  if ($view->id() == 'name_of_your_view' && $view->current_display == 'machine_name_of_your_view_page') {
    $tid = $view->args[0];
    $view->setTitle(Term::load($tid)->label());
  }
}

Or any variation of this, depending on where your contextual filter is in the args, or what you want to put into the title. And if you have more than one view, you have to cover all of them. And if you change the name or add another view, you have to keep the code in sync. And if you're one of those who only mouse around in Drupal but won't write your own code, well, you're hosed. :-)

int_ua’s picture

Thank you, now I'll just have to figure out how to add i18n support.

Edit

Posted my code here: https://drupal.stackexchange.com/a/298186/7177

djg_tram’s picture

To put this into perspective, the proper way to do this would be to write a route enhancer and, in its `enhance()` function, set your title with:

$defaults['_title'] = ...whatever

The problem is a still outstanding bug: https://www.drupal.org/project/drupal/issues/2981183 causing the system not to pick up the modified title. Hence the need for this view altering hack. If it worked, Views could probably incorporate it easily by default.

bhanojeerao’s picture

I faced same issue, #9 provided by maxdev is solved my issue.

Shriaas’s picture

I was facing the same issue and since I want to show a custom entity on the taxonomy page not "node" solution in #9 didn't work for me so instead I added a relationship using my term reference field and then used Taxonomy term: Term ID in contextual filter.

rossidrup’s picture

i like solution #16 and I created relationship with field_catalog: Taxonomy term
then I created a contextual filter (field_catalog: Taxonomy term) Taxonomy term: Name

and I see after the replaced name has this

replacement tokens

{{ arguments.field_catalog_target_id }} == Product: Catalog (field_catalog) title
{{ raw_arguments.field_catalog_target_id }} == Product: Catalog (field_catalog) input
{{ arguments.name }} == Taxonomy term: Name title
{{ raw_arguments.name }} == Taxonomy term: Name input

I chose {{ arguments.field_catalog_target_id }} to Override title of the view
but it aint working

I also tried
{{ arguments.name }} and not working....still figuring out....Drupal 7 got that working nicely....
so this is still unresolved?

I also added new field and it shows up the category name very well, but only in field not in the view name
(field_catalog: Taxonomy term) Taxonomy term: Name

Basically my product terms are called Catalog (earrings, rings, pendants) ......

I created 3 contextual filters
Product: Catalog
(field_catalog: Taxonomy term) Taxonomy term: Name
(field_catalog: Taxonomy term) Taxonomy term: Term ID

and tried replacement patterns, nothing works

I have 6 replacements patterns now and none of them works
{{ arguments.field_catalog_target_id }} == Product: Catalog (field_catalog) title
{{ raw_arguments.field_catalog_target_id }} == Product: Catalog (field_catalog) input
{{ arguments.name }} == Taxonomy term: Name title
{{ raw_arguments.name }} == Taxonomy term: Name input
{{ arguments.tid }} == Taxonomy term: Term ID title
{{ raw_arguments.tid }} == Taxonomy term: Term ID input

i confirm this is a bug, because it should show up

https://pasteboard.co/zEQzZZYJ91v2.png

clau_bolson’s picture

rossidrup: This works:

https://www.drupal.org/forum/support/post-installation/2013-03-15/how-to...

Keep your relationship, erase all of your contextual filters, add one to field-catalog: Taxonomy Term: Term ID
And replace title with {{ arguments.tid }}
That's all!

P.Selfin’s picture

Solution https://www.drupal.org/project/drupal/issues/2912332#comment-12898360 works foe me without any patch. Drupal 9.4.5

adaddinsane’s picture

I was using two custom entities and attempting to change the title - the "proper" solution doesn't work because the custom entities (as generated by Drupal console) don't have a title, they have a name.

I had to use the solution in #22, adapted for the correct entity names.

chike’s picture