Invoke hook_entity_view_mode_alter().
If adding a new entity similar to nodes, comments or users, you should invoke this function during the ENTITY_build_content() or ENTITY_view_multiple() phases of rendering to allow other modules to alter the view mode during this phase. This function needs to be called before field_attach_prepare_view() to ensure that the correct content is loaded by field API.
$entity_type: The type of entity, i.e. 'node', 'user'.
$entities: The entity objects which are being prepared for view, keyed by object ID.
$view_mode: The original view mode e.g. 'full', 'teaser'...
$langcode: (optional) A language code to be used for rendering. Defaults to the global content language of the current request.
An associative array with arrays of entities keyed by view mode.
function entity_view_mode_prepare($entity_type, $entities, $view_mode, $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
// To ensure hooks are never run after field_attach_prepare_view() only
// process items without the entity_view_prepared flag.
$entities_by_view_mode = array();
foreach ($entities as $id => $entity) {
$entity_view_mode = $view_mode;
if (empty($entity->entity_view_prepared)) {
// Allow modules to change the view mode.
$context = array(
'entity_type' => $entity_type,
'entity' => $entity,
'langcode' => $langcode,
);
drupal_alter('entity_view_mode', $entity_view_mode, $context);
}
$entities_by_view_mode[$entity_view_mode][$id] = $entity;
}
return $entities_by_view_mode;
}