function hook_ENTITY_TYPE_view_mode_alter

Same name in other branches
  1. 10 core/lib/Drupal/Core/Entity/entity.api.php \hook_ENTITY_TYPE_view_mode_alter()

Change the view mode of a specific entity type currently being displayed.

Parameters

string $view_mode: The view_mode currently displaying the entity.

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

Related topics

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1740

Code

function hook_ENTITY_TYPE_view_mode_alter(string &$view_mode, \Drupal\Core\Entity\EntityInterface $entity) : void {
    // Change the view mode to teaser.
    if ($view_mode == 'full') {
        $view_mode = 'teaser';
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.