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?


1 Answer 1


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


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