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

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.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.