5

So my question is how I can get programmatically in preprocess node hook, the display mode of a node, for example, to check if view mode is a teaser or full?

1 Answer 1

8

The view mode is in the vars:

function MYMODULE_preprocess_node(&$vars) {
    $view_mode = $vars['view_mode'];
}

See template_preprocess_node() or node.html.twig for more info.

10
  • Thanks for respond Clive. Exactly what I've needed so proper condition would be $view_mode = $vars['view_mode']; if ($view_mode == 'easer') { Do something } Commented Mar 17, 2017 at 20:07
  • 2
    Exactly right :) (well, you might want a "t" in the "easer" but I'm guessing you know tha)
    – Clive
    Commented Mar 17, 2017 at 20:10
  • @leymannx Just FYI I reverted that last edit, I didn't see the improvement in it - preprocess hooks are equally valid in modules or themes
    – Clive
    Commented Nov 29, 2018 at 11:36
  • 1
    @leymannx If memory serves this goes back to a convention that grew up from D6 - template_preprocess_* is basically Drupal's way of saying this theme has an implied, required preprocess that sets up variables, and here it is. If I had to guess, I'd say it was to ensure that those required variables would be available to modules/themes subsequently implementing the preprocess hook. Otherwise, if it was node_preprocess_node, for example, the node module would have to fight with the other modules implementing the same hook in terms of module weight. foo_preprocess_node...
    – Clive
    Commented Nov 29, 2018 at 11:56
  • 2
    ...wouldn't have access to $vars['node'], and so on
    – Clive
    Commented Nov 29, 2018 at 11:56

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.