I've got a bizarre issue where my the render array from my block plugin's build() method doesn't seem to be properly interpreted by the template. I've got the following setup.
function my_module_theme(){
return [
'block__my_module__user_header' => [
'render element' => 'children', // also tried without this
'variables' => [
'tabs' => NULL,
'user' => NULL,
],
],
]
}
and my block plugin class
public function build() {
$render_array = [
'#theme' => 'block__my_module__user_header',
'#tabs' => $this->getTabs(), // returns an array of data to handled in twig
'#cache' => [
'contexts' => [
'route',
'user',
],
],
];
if ($user = $this->getContextValue('user')) {
$render_array['#cache']['tags'] = $user->getCacheTags();
$render_array['#user'] = $user;
}
return $render_array;
}
But then, when I inspect the variables in theme_preprocess_block(), all of that render array information from build() is sitting in $variables['content']. Same in the actual twig template, and same result if I remove ['#cache']
from build(). I'd expect to be able to access {{ tabs }}
directly, but instead the data is sitting in {{ content['#tabs'] }}
. Aside from that inconvenience, it also means that any cache tags/contexts I try to set in build() don't end up in the actual render array so they're completely ignored, which is a problem because the "tabs" as rendered in twig are dependent on this context.
I'm completely perplexed, as the same pattern works fine for hook_theme/block plugins in some of my modules, but this issue crops up in other modules.