We have to get the current node's view_mode from within the mymodule_preprocess_page(&$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(&$variables) hook?

Here is what we tried to do

function mymodule_preprocess_page(&$variables) {
  $node = $variables['node'];
  if(isset($node) && is_object($node) {
    // Tried printing variables but cannot find `view_mode`
  • @4k4 I updated the question with more information regarding our pages. We essentially added an entity reference to the basic page so we just want to be sure the hook is only run once. – usernameabc Mar 14 '19 at 21:27

It's possible you are using the wrong hook. hook_preprocess_page() is not for the content type page. Use a preprocess hook for nodes and check the content type:

function mymodule_preprocess_node(&$variables) {
  if ($variables['node']->getType() === 'page') {
    $view_mode = $variables['view_mode'];
    // preprocess only nodes of the content type "Basic Page"

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->ds_switch->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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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