1 entity.api.php hook_entity_view_mode_alter(&$view_mode, $context)

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

Parameters

string $view_mode: The display mode that is to be used to display the entity. Note: this variable is passed by reference, and changing the value will change the display mode that is used by the calling function.

array $context: Array with contextual information, including:

  • entity_type: The type of the entity that is being viewed.
  • entity: The entity object.
  • langcode: The langcode the entity is being viewed in.

Related topics

File

core/modules/entity/entity.api.php, line 512
Hooks provided by the Entity module.

Code

function hook_entity_view_mode_alter(&$view_mode, $context) {
  // For nodes, change the display mode when it is teaser.
  if ($context['entity_type'] == 'node' && $view_mode == 'teaser') {
    $view_mode = 'my_custom_view_mode';
  }
}