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

2 functions implement hook_ENTITY_TYPE_view_mode_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

entity_test_entity_test_view_mode_alter in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_ENTITY_TYPE_view_mode_alter().
node_test_entity_view_mode_alter in core/modules/node/tests/modules/node_test/node_test.module
Implements hook_entity_view_mode_alter().

File

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

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';
  }
}