function hook_entity_view_mode_alter

hook_entity_view_mode_alter(&$view_mode, $context)

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.

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

modules/system/system.api.php, line 480
Hooks provided by Drupal core and the System module.

Code

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

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_entity_view_mode_alter/7.x