With Commerce Product taxonomy term view, is there a way to have page title set to the translated term title?
-
28-02-2021 - |
문제
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):
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.
해결책
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());
}
}