1

This question already has an answer here:

I have a content type with a custom view mode. How do I make sure node always get rendered with my custom view mode?

What would be the right way to programmatically change the node view mode before rendering?

marked as duplicate by leymannx, mradcliffe, Felix Eve, Alfred Armstrong, googletorp Oct 8 '18 at 13:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

If you want to make sure that a content type is always rendered with a custom view mode, use the following hook in a module. (I assume the module machine name is mymodule.)

mymodule.module

/**
 * Implements hook_entity_view_mode_alter().
 */
function mymodule_entity_view_mode_alter(&$view_mode, $entity, $context) {
  if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'my_content_type') {
    $view_mode = 'my_custom_view_mode';
  }
}

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