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')

   foreach ($view_modes as $view_mode) {
   $view_mode = str_replace('node.', '', $view_mode);
   $suggestions[] = 'block--' . $node->getType() .'-'. $view_mode;
  • Still interested to know above, but I realized for the title I can just have the title block not display on the content type and the title will be rendered via template elsewhere Commented Dec 17, 2018 at 21:24

1 Answer 1


You say in your comment it is not useful for you and I doubt there are many use cases for this at all, because normally as main content a node is rendered in the full view mode.

Nonetheless trying to answer: A block is built based on contexts and at the moment this is the route, which you have to add as cache metadata to the block. You can then base the decision which view mode the node uses on this route by checking _entity_view of the route definition. If the view mode is controlled by code somewhere else you need to pull this in as context as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.