Use this to enforce a particular view mode or to test your view modes.
<?php
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_entity_view_mode_alter().
*/
function MYMODULE_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'news' && $view_mode == 'full') {
$view_mode = 'some_different_view_mode';
}
}
Useful when you are creating a "search index" view mode and want to make sure it renders correctly.
Add new comment