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.

Comments

Sinan Erdem created an issue. See original summary.

websiteworkspace’s picture

I 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.

damienmckenna’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.4.x-dev
Component: taxonomy data » taxonomy.module

In D8 the Views module was moved into core.

websiteworkspace’s picture

Yes, 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.

damienmckenna’s picture

Component: taxonomy.module » views_ui.module
Sinan Erdem’s picture

@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).

websiteworkspace’s picture

@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.

Sinan Erdem’s picture

DOCUMENTATION: How about putting this info under the title textbox of the view?

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

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

marco-s’s picture

The workaround from #6 works like a charm!
@websiteworkspace: thanks for the further explanation!

sam711’s picture

The 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 }}'

davituri’s picture

Another (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) }}

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

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

matsbla’s picture

iamdroid’s picture

#12 helped me. Thanks.

niklan’s picture

Same 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.

websiteworkspace’s picture

The 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 }}

andileco’s picture

I've created a sandbox for doing this in a language-aware way: https://www.drupal.org/sandbox/andileco/3021338

maria.dis’s picture

Im 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

<

div{{ attributes.setAttribute('id', 'taxonomy-term-' ~ term.id).addClass(classes) }}>
  {{ title_prefix }}
  {% if not page %}
    <h2><a href="{{ url }}">{{ name }}</a></h2>
  {% endif %}
  {{ title_suffix }}

// I added this div class with {{name}}

 <strong> <div class="taxonomy-title-header">{{ name }}</div></strong>

// end

  <div class="content">
    {{ content }}
  </div>
</div>

And magicly the title shows in the top of the view page. I hope that helps.

plach’s picture

Title: D8 - Set the view title to term name when filter argument is term id » Allow entity reference views arguments to display the entity label in titles and summaries
Version: 8.6.x-dev » 8.8.x-dev
Component: views_ui.module » views.module
Category: Bug report » Feature request
Status: Active » Needs review
StatusFileSize
new82.51 KB
new7.14 KB

Here'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:

yogeshmpawar’s picture

Assigned: Unassigned » yogeshmpawar
yogeshmpawar’s picture

Assigned: yogeshmpawar » Unassigned
StatusFileSize
new7.22 KB

Re-rolled the patch against 8.8.x branch.

plach’s picture

Rerolled

Status: Needs review » Needs work

The last submitted patch, 23: views-entity_reference_argument-2912332-24.patch, failed testing. View results

yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new7.91 KB
new739 bytes

Hopefully, the updated patch will solve the failed test issue. added an interdiff as well.

plach’s picture

Thanks!

matsbla’s picture

I 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.

plach’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

@matsbla:

I think it would make sense to also asd this for argument "Has taxonomy term ID"

I think I'd be fine either way. It's unlikely I'll have the time to work on that myself though.

plach’s picture

Minor performance improvement.

plach’s picture

StatusFileSize
new1.03 KB

(forgot the interdiff)

brayfe’s picture

The 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.

dunebl’s picture

#29 working so nicely!!!
Thank you

daffie’s picture

A short review. Patch looks good.

  1. +++ b/core/modules/views/src/Plugin/views/argument/EntityReferenceArgument.php
    @@ -0,0 +1,199 @@
    +<?php
    ...
    +class EntityReferenceArgument extends NumericArgument implements ContainerFactoryPluginInterface {
    

    Just to be sure. Entity references can only be of the type numeric?

  2. +++ b/core/modules/views/src/Plugin/views/argument/EntityReferenceArgument.php
    @@ -0,0 +1,199 @@
    +    /** @noinspection PhpParamsInspection */
    

    This line can be removed.

  3. +++ b/core/modules/views/src/Plugin/views/argument/EntityReferenceArgument.php
    @@ -0,0 +1,199 @@
    +   * @throws \Exception
    

    Why? And can this be solved without throwing an exception.

  4. +++ b/core/modules/views/src/Plugin/views/argument/EntityReferenceArgument.php
    @@ -0,0 +1,199 @@
    +
    ...
    +    $id = $data->{$this->name_alias};
    

    What happens when $data->{$this->name_alias} is not set?

  5. +++ b/core/modules/views/src/Plugin/views/argument/EntityReferenceArgument.php
    @@ -0,0 +1,199 @@
    +   * @throws \Exception
    

    Why? And can this be solved without throwing an exception.

  6. +++ b/core/modules/views/src/Plugin/views/argument/EntityReferenceArgument.php
    @@ -0,0 +1,199 @@
    +   * @throws \Exception
    

    Why? And can this be solved without throwing an exception.

  7. +++ b/core/modules/views/views.views.inc
    @@ -833,6 +833,11 @@ function core_field_views_data(FieldStorageConfigInterface $field_storage) {
    +    // Add a dedicated entity reference argument handler.
    +    $field_columns = $field_storage->getColumns();
    +    $column_real_name = $table_mapping->getFieldColumnName($field_storage, key($field_columns));
    +    $data[$table_name][$column_real_name]['argument']['id'] = 'entity_reference';
    

    I am not completely sure about this code. Can somebody give me some explanation.

ckaotik’s picture

1. Just to be sure. Entity references can only be of the type numeric?

\Drupal\views\EntityViewsData::processViewsDataForEntityReferenceassumes this is the case for all content entity types. So I guess it's safe to assume the same.

7. [...] I am not completely sure about this code.

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_alter already 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)?

To preserve BC, since people may rely on the workarounds listed above, labels are only displayed if the related option is enabled

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_id with raw_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?

Dave_Nichols’s picture

Thanks. #25 worked great for me on Drupal 8.7.7

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

sorlov’s picture

Status: Needs work » Needs review
StatusFileSize
new8.6 KB

Config schema is missing for *use_label* field, so got error when validating config with Configuration inspector

Updated patch from #29

Status: Needs review » Needs work

The last submitted patch, 37: views-entity_reference_argument-2912332-37.patch, failed testing. View results

dunebl’s picture

#37 failed on 8.8

atching file core/modules/views/config/schema/views.argument.schema.yml
patching file core/modules/views/src/EntityViewsData.php
Hunk #1 FAILED at 617.
1 out of 1 hunk FAILED -- saving rejects to file core/modules/views/src/EntityViewsData.php.rej
patching file core/modules/views/src/Plugin/views/argument/EntityReferenceArgument.php
patching file core/modules/views/tests/src/Unit/EntityViewsDataTest.php
Hunk #1 succeeded at 1159 (offset 25 lines).
patching file core/modules/views/views.views.inc
Hunk #1 succeeded at 891 (offset 58 lines).
dunebl’s picture

#29 failed also on 8.8.2

patching file core/modules/views/src/EntityViewsData.php
Hunk #1 FAILED at 610.
1 out of 1 hunk FAILED -- saving rejects to file core/modules/views/src/EntityViewsData.php.rej
patching file core/modules/views/src/Plugin/views/argument/EntityReferenceArgument.php
patching file core/modules/views/tests/src/Unit/EntityViewsDataTest.php
Hunk #1 succeeded at 1159 (offset 54 lines).
patching file core/modules/views/views.views.inc
Hunk #1 succeeded at 891 (offset 58 lines).

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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

Status: Needs work » Postponed

Most 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.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

p.selfin’s picture

Solution #16 works foe me without any patch. Drupal 9.4.5

pameeela’s picture

Status: Postponed » Closed (duplicate)

Closing 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.

pameeela’s picture

dww’s picture

FYI: 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

dww’s picture

Category: Feature request » Bug report

p.s. Doesn't really matter, but I'm restoring this to a bug. 😅

hongqing’s picture

I still have the issue with D10.3.0