In my code, I need to get the current view mode of the node to be able to pass it to be used as a block template suggestion.
The function I have set up is able to grab all available view modes, but I just want the current one. Possibly a condition that I haven't come across yet would do the trick?
function mytheme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
// Add template suggestions based on the current view mode and node type.
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$view_modes = Drupal::entityQuery('entity_view_mode')
->condition('targetEntityType', 'node')
->execute();
foreach ($view_modes as $view_mode) {
$view_mode = str_replace('node.', '', $view_mode);
$suggestions[] = 'block--' . $node->getType() .'-'. $view_mode;
}
}
}