I have a very simple (block) view to list a node's title. This view has an id from url contextual filter and a single filter to filter on Content language set for the page (by URL prefix). This block is then placed using Layout Builder on the Full Content view mode for this node type. The view is set to use Tag based caching. When I switch the page back and forth between languages; the title language does not change.
Disabling views caching fixes this.
A little digging and I see that Views cache context is based on Interface language but the Title is content; so it needs to be based on Content language. I have seen a few posts on setting cache context and cache tags but those don't seem to work.
So far I have tried this:
function mycustom_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
$language_manager = \Drupal::languageManager();
$langcode = $language_manager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
if (isset($view->element['#cache']['contexts']) && is_array($view->element['#cache']['contexts'])) {
foreach ($view->element['#cache']['contexts'] as $key => $context) {
if (stristr($context, 'language_interface')) {
unset($view->element['#cache']['contexts'][$key]);
}
}
$view->element['#cache']['contexts'][] = 'languages:language_content';
$view->element['#cache']['tags'][] = 'language:' . $langcode;
}
}
None of this seems to help.
Is it possible I have fixed the view caching but the block is still not cached properly?