How to change the view mode programmatically? I want to change the view mode of node programmatically. I used hook_preprocess_node(&amp;$vars) and set the view mode:

function moduleName_preprocess_node(&amp;$vars) {
      // if conditions to apply particular view mode
      $vars['view_mode'] = 'teaser';
      $vars['elements']['#view_mode'] = 'teaser';
}


But this is not working as expected. Am I missing something here?
Altering the view mode in the preprocess function is doing it too late.
The correct way to alter the view module is implementing hook_entity_view_mode_alter(), which is called before the entity content is rendered. For example, in node_view(), that hook is invoked from node_build_content(), which is the first function called from node_view().

  if (!isset($langcode)) {
    $langcode = $GLOBALS['language_content']-&gt;language;
  }

  // Populate $node-&gt;content with a render() array.
  node_build_content($node, $view_mode, $langcode);