Alter the presentation (view mode) of an entity in its preview

03/05/2016

This is how to modify the presentation (view mode) of an entity according to the conditions that we need through the implementation of a hook_entity_view_mode_alter.  In practice, I have utilized it to change the presentation (view mode) of the user entity when the logged in user is distinct from the user whose profile is being visualized.  In other words, when the active user is viewing the profile of another user.

/**
 * Implements hook_entity_view_mode_alter.
 */
function my_module_entity_view_mode_alter(&$view_mode, $context) {
  // If entity_type is user show 'example' view mode.
  if ($context['entity_type'] == 'user') {
    $view_mode = 'example';
  }
}

You can also add a personalized presentation to an entity (view mode) in it visualization.  I teach you how to do it in this snippet.