I have a multilingual site with custom blocks in the right-hand menu - the content of which is generated dynamically through the build()
function of BlockBase and which changes depending on the interface language. I want to cache these once per language. Elsewhere I've used:
public function build() {
$node = \Drupal::routeMatch()->getParameter('node');
return array(
'#theme' => 'my_template',
....
'#cache' => [
'tags' => [
'node:' . $node->getId()
],
],
);
}
I've gone with this approach, which appears to work, although I'm not sure how to test that it isn't still processing templates on every call:
public function build() {
return array(
'#theme' => 'my_template',
....
'#cache' => [
'tags' => [
'language:' . \Drupal::languageManager()->getCurrentLanguage()->getId()
],
],
);
}
Is this the correct way to do it? I could find examples of the node:
prefix in the docs, but nothing about language: