/Drupal 8

function hook_entity_view_mode_alter

hook_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context)

Change the view mode of an entity that is being displayed.


string $view_mode: The view_mode that is to be used to display the entity.

\Drupal\Core\Entity\EntityInterface $entity: The entity that is being viewed.

array $context: Array with additional context information, currently only contains the langcode the entity is viewed in.

Related topics

Entity CRUD, editing, and view hooks
Hooks used in various entity operations.
Define functions that alter the behavior of Drupal core.


core/lib/Drupal/Core/Entity/entity.api.php, line 1463
Hooks and documentation related to entities.


function hook_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) {
  // For nodes, change the view mode when it is teaser.
  if ($entity->getEntityTypeId() == 'node' && $view_mode == 'teaser') {
    $view_mode = 'my_custom_view_mode';

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.