I have a site that uses hook_preprocess_block to add a node content type/bundle variable for a block twig template. The template then has logic to display different HTML depending on the node type.
The problem is that caching is breaking this. Whichever variation is cached first is always shown. I think I want to set the cache context for this but not seeing a "node content type" context.
I see some caching related code in the function (I inherited this project) but not sure what it's doing.
function THEME_preprocess_block(&$variables) {
if ($variables['base_plugin_id'] == 'block_content') {
$renderer = \Drupal::service('renderer');
$renderer->addCacheableDependency($variables, $variables['content']['#block_content']);
}
$block_type = $variables['elements']['#configuration']['label'];
if ($block_type == 'Footer Content Block') {
$node = \Drupal::routeMatch()->getParameter('node');
$variables['node_type'] = $node->bundle();
}
}