With Commerce Product taxonomy term view, is there a way to have page title set to the translated term title?

drupal.stackexchange https://drupal.stackexchange.com/questions/297875

Question

The only way to have the taxonomy term name in the title of a taxonomy term Commerce Products view page I've found so far is the example view in the estore theme (I still don't get how tid which should stand for Term ID becomes the title field value):

screenshot of the View  loading= Advanced settings">

screenshot of contextual filter: Taxonomy term: Term ID

The problem is that the title shown is not translated on multilingual sites when the term definitely has a proper translation. Is there any way to show that title translated?

For some reason there are no Product: Has taxonomy term ID as there is Content: Has taxonomy term ID in the default taxonomy term view that is displaying the title translated.

AFAIU {{ arguments.tid|t }} is not the way because it will go through a different subsystem.

Was it helpful?

Solution

I had to workaround it in code by modifying an example posted by djg_tram:

/**
 * @file
 * Workaround for taxonomy term title not being i18zed.
 */
use Drupal\views\ViewExecutable;
use Drupal\taxonomy\Entity\Term;

/**
 * Implements hook_views_pre_render().
 */
function exposed_filter_workaround_views_pre_render(ViewExecutable $view) {
  $view_id = 'products';
  $view_display = 'page_category';

  if ($view->id() == $view_id && $view->current_display == $view_display) {
    $lang_id = \Drupal::languageManager()->getCurrentLanguage()->getId();

    $term_id = $view->args[0];
    $term = Term::load($term_id);

    $term_translated = $term->getTranslation($lang_id);
    $view->setTitle($term_translated->label());
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top