Get node's view mode within template_preprocess_page We have to get the current node's view_mode from within the mymodule_preprocess_page(&amp;$variables) hook. When we tried the following we could not get the view_mode after performing some validation to make sure we have a node object. 

In the Basic page, we have a Related Articles section that is an entity reference to the Article content type. This returns multiple nodes depending upon the page and we want to ensure the template_preprocess_node() hook is ran only once. 

How can we get the current node's view_mode from within the template_preprocess_page(&amp;$variables) hook?

Here is what we tried to do

function mymodule_preprocess_page(&amp;$variables) {
  $node = $variables['node'];
  if(isset($node) &amp;&amp; is_object($node) {
    // Tried printing variables but cannot find `view_mode`
    ksm($node);
  }
}
I needed to put a class based on view mode onto my pages.  I seem to remember that view mode is handled separately from the node data.  My solution was actually based on using the Display Suite submodule Switch View Mode (was already using DS).  This module creates a new base field in the entity and sets the view mode to it.

This let me use the following code to create the class:

$view_mode = $node-&gt;ds_switch-&gt;value;
if (!empty($view_mode)) {
   $variables['page_class'] .= " node-view-{$view_mode}";
}


This solution won't work with out the module.  A bit of tracing on how this works shows that it is based on using hook_entity_view_mode_alter() to get the view mode and add it to the ds_switch field on the node. 

This field seems to be added by using the hook_entity_base_field_info().  See the ds_switch_view_mode.module file in the Display Suite code for details on this.