Related issue for D6 and D7 here:
#1211264: Set the view title to term name when filter argument is term id
In D8, the issue seems to continue. I am actually trying to override term pages for Products (Commerce). So, the Product entity doesnt have the usual "Has Taxonomy Term" filter. Instead I am using term field. When I try to override title, i am using twig as:
{{ arguments.field_kategori_target_id }}
I also do validation with Term ID and select "One or more IDs separated by , or +"
The title shows term ID instead of term NAME.
| Comment | File | Size | Author |
|---|---|---|---|
| #37 | views-entity_reference_argument-2912332-37.patch | 8.6 KB | sorlov |
| #30 | interdiff.txt | 1.03 KB | plach |
| #29 | views-entity_reference_argument-2912332-29.patch | 7.99 KB | plach |
Comments
Comment #2
websiteworkspace commentedI too have experienced this same problem.
After selecting "override title" in the contextual filter, only the following twig replacement values are available:
{{ arguments.field_taxo_term_prod_category_target_id }}
{{ raw_arguments.field_taxo_term_prod_category_target_id }}
The foregoing values output the term ID in the view title, not the term name.
So, for example, when working with D8 commerce products with a taxonomy field for product category, the result is something like:
Products in Category - 21
where 21 is the term ID of a Product category, when it should display the term name. The 99.999999% use case is that the output should be:
Products in Category - Category0001
-
View ought to provided replacement values like:
{{ arguments.field_taxo_term_prod_category_target_name }}
{{ raw_arguments.field_taxo_term_prod_category_target_name }}
not the ID values it currently provides.
This is a serious bug for which no workaround I have tried works.
It is just bizarre that a basic bug (feature omission) like this has existed for two years since the Drupal 8.0.0 release.
{{ arguments.field_taxo_term_prod_category_target_id }}
{{ raw_arguments.field_taxo_term_prod_category_target_id }}
The functional features of the View operate correctly, except this one, which makes the View rather useless to work since, the dynamic title of the View is essential to its presentation.
Comment #3
damienmckennaIn D8 the Views module was moved into core.
Comment #4
websiteworkspace commentedYes, of course we know that views is in Drupal 8.x core.
Someone would have be brain dead not to know that.
This is all the more reason why this bug should have been fixed years ago.
It is absolutely astonishing that this bug, (this omission of an essential feature), has existed for all the years that Drupal 8.x has been in development and production.
This frequently used/needed type of Views contextual filter is basically impossible without:
{{ arguments.field_(some_taxonomy_field)_target_name }}
{{ raw_arguments.field_(some_taxonomy_field)_target_name }}
available as replacement values for use with the Views contextual filter "override title" feature.
Comment #5
damienmckennaComment #6
Sinan Erdem commented@DrupalSiteBuilder, Drupal is a huge open-source project. Some bugs can stay for a long time or reappear. Even in Linux, there are such issues (https://www.theregister.co.uk/2017/02/23/linux_kernel_gets_patch_against...)
Related with the issue, I have found a workaround. You can use a replacement pattern from a row, in the title. Just add the needed field (make it hidden if you like), then get the replacement pattern (example {{ field_name }} ). You can use this pattern directly in the title of the view (not the contextual filter area).
Comment #7
websiteworkspace commented@Sinan Erdem
Been around open source for many years, knowing a lot about Drupal, and the machinery of getting bugs fixed.
Meanwhile, THANK YOU !!!!!!
Your workaround works!
Remembering now that replacement patterns from the available fields can be used in a View title!
1. Add the needed field to the field list and set it to hidden if needed.
Then, for the title of the View that traverses the product categories via /taxonomy/term/%
The view title:
Products in {{ field_taxo_term_prod_category }}
Works like a charm, showing the name of the current taxonomy term in the title dynamically.
Comment #8
Sinan Erdem commentedDOCUMENTATION: How about putting this info under the title textbox of the view?
Comment #10
marco-sThe workaround from #6 works like a charm!
@websiteworkspace: thanks for the further explanation!
Comment #11
sam711 commentedThe workaround from #6 worked well for me as well.
But if Breadcrumbs block is used, it doesn't get the twig parsed and shows like 'Home > {{ name }}'
Comment #12
davituri commentedAnother (very hacky) workaround is to create a custom twig extension that gets TID and returns term name (Here is a sample)
With that, you can put this in Override Title field:
{{ term_name(arguments.field_tags_target_id) }}Comment #14
matsbla commentedComment #15
iamdroid commented#12 helped me. Thanks.
Comment #16
niklanSame as #12, we can use Twig Tweak:
{{ drupal_field('name', 'taxonomy_term', arguments.field_tags_target_id) }}Solution from #6 is not reliable with multiple entity reference field, it gets first entity from the field value of the last entity on the page, this can be different term from page the user is on now.
Also, Twig Tweak takes care about language of the page is loading and term field value translation for this language. So if you are using Twig Tweak already, this is the easiest and fastest solution.
Comment #17
websiteworkspace commentedThe following module helps with this.
https://www.drupal.org/project/product_taxonomy_filter
When using the above module the [ override title ] feature on the view's contextual filter may be working correctly now with the token:
{{ arguments.term_product_tid_depth }}
Comment #18
andileco commentedI've created a sandbox for doing this in a language-aware way: https://www.drupal.org/sandbox/andileco/3021338
Comment #19
maria.dis commentedIm using drupal 8.5 with bootstrap barrio 4 theme. The defauld taxonomy view does not show the Title name of the taxonomy either. In "override title" in the contextual filter, by default show this value {{ arguments.tid }}.
So I opened the twig theme/bootstrap_barrio/template/content/taxonomy-term.html.twig
<
And magicly the title shows in the top of the view page. I hope that helps.
Comment #20
plachHere's a first attempt at implementing this. To preserve BC, since people may rely on the workarounds listed above, labels are only displayed if the related option is enabled:
Comment #21
yogeshmpawarComment #22
yogeshmpawarRe-rolled the patch against 8.8.x branch.
Comment #23
plachRerolled
Comment #25
yogeshmpawarHopefully, the updated patch will solve the failed test issue. added an interdiff as well.
Comment #26
plachThanks!
Comment #27
matsbla commentedI tested and it works great!
I think it would make sense to also asd this for argument "Has taxonomy term ID" where I guess in many cases it also would make sense to display term name rather than the ID in title.
Comment #28
plach@matsbla:
I think I'd be fine either way. It's unlikely I'll have the time to work on that myself though.
Comment #29
plachMinor performance improvement.
Comment #30
plach(forgot the interdiff)
Comment #31
brayfe commentedThe patch in #29 worked well for me! Using Drupal 8.7.3, and a view block with a contextual filter for taxonomy terms to override the block title with the term label versus term id.
Comment #32
dunebl#29 working so nicely!!!
Thank you
Comment #33
daffie commentedA short review. Patch looks good.
Just to be sure. Entity references can only be of the type numeric?
This line can be removed.
Why? And can this be solved without throwing an exception.
What happens when $data->{$this->name_alias} is not set?
Why? And can this be solved without throwing an exception.
Why? And can this be solved without throwing an exception.
I am not completely sure about this code. Can somebody give me some explanation.
Comment #34
ckaotik\Drupal\views\EntityViewsData::processViewsDataForEntityReferenceassumes this is the case for all content entity types. So I guess it's safe to assume the same.Me too, because this doesn't apply the logic from 1. and changes the argument for config entities, too. I don't think config entities are supported by the patch (yet).
8. I'm wondering how well this works for taxonomy reference fields.
taxonomy_field_views_data_alteralready adds a custom filter (but forgets the argument handler, that the module also provides). I also find the implementation of that argument handler (Drupal\taxonomy\Plugin\views\argument::IndexTid) quite elegant, can't we go about it the same way (without the bloat)?9. I find this setting rather annoying, since it's redundant with the chosen argument (vs. raw argument). How about we instead use a (post?) update hook that replaces all ocurrences of
arguments.field_foo_target_idwithraw_arguments.field_foo_target_id? That way, existing views will still work as expected (they're getting the entity id), while the argument title behaves as it should have from the beginning?Comment #35
Dave_NicholsThanks. #25 worked great for me on Drupal 8.7.7
Comment #37
sorlov commentedConfig schema is missing for *use_label* field, so got error when validating config with Configuration inspector
Updated patch from #29
Comment #39
dunebl#37 failed on 8.8
Comment #40
dunebl#29 failed also on 8.8.2
Comment #42
colanMost of this appears to duplicate the work in #2640994: Fix label token replacement for views entity reference arguments, but is less comprehensive. So let's get that in first, and then we can see what's left to do here.
Comment #47
p.selfin commentedSolution #16 works foe me without any patch. Drupal 9.4.5
Comment #48
pameeela commentedClosing in favour of #2640994: Fix label token replacement for views entity reference arguments which has green (and working) patches for 9.3.x and 9.4.x.
Comment #49
pameeela commentedComment #50
dwwFYI: Both #2640994: Fix label token replacement for views entity reference arguments and #3442227: Use labels in Views argument summaries for entity references are committed and will be available in 10.3.0 and up. 🎉 At last, these bugs are fixed! We copied over credit for the effort here to #3442227, too.
Enjoy,
-Derek
Comment #51
dwwp.s. Doesn't really matter, but I'm restoring this to a bug. 😅
Comment #52
hongqing commentedI still have the issue with D10.3.0