function hook_entity_view_mode_alter

Same name in other branches
  1. 7.x modules/system/system.api.php \hook_entity_view_mode_alter()
  2. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_view_mode_alter()
  3. 8.9.x core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_view_mode_alter()
  4. 11.x core/lib/Drupal/Core/Entity/entity.api.php \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.

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 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 1690

Code

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

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