function hook_entity_view_mode_alter

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

Parameters

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

1 function implements hook_entity_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.

node_test_entity_view_mode_alter in drupal/core/modules/node/tests/modules/node_test/node_test.module
Implements hook_entity_view_mode_alter().

File

drupal/core/includes/entity.api.php, line 274
Hooks provided the Entity module.

Code

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
    ->entityType() == 'node' && $view_mode == 'teaser') {
    $view_mode = 'my_custom_view_mode';
  }
}