diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 5db5442746d570b86670eb655d239c6b0e06250c..aef62094afa18944b685398392780e81c423f0f2 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1050,6 +1050,10 @@ function comment_build_content(Comment $comment, Node $node, $view_mode = 'full' // Remove previously built content, if exists. $comment->content = array(); + // Allow modules to change the view mode. + $context = array('langcode' => $langcode); + drupal_alter('entity_view_mode', $view_mode, $comment, $context); + // Build fields content. field_attach_prepare_view('comment', array($comment->cid => $comment), $view_mode, $langcode); entity_prepare_view('comment', array($comment->cid => $comment), $langcode); diff --git a/core/modules/entity/entity.api.php b/core/modules/entity/entity.api.php index 98e32b842365049776a4bb857fef8edce4ac28fc..bd5d1477fcb10cc162e4e64664511e4beadead5f 100644 --- a/core/modules/entity/entity.api.php +++ b/core/modules/entity/entity.api.php @@ -448,3 +448,21 @@ function hook_entity_prepare_view($entities, $entity_type) { } } } + +/** + * Change the view mode of an entity that is being displayed. + * + * @param string $view_mode + * The view_mode that is to be used to display the entity. + * @param Drupal\entity\EntityInterface $entity + * The entity that is being viewed. + * @param array $context + * Array with additional context information, currently only contains the + * langcode the entity is viewed in. + */ +function hook_entity_view_mode_alter(&$view_mode, Drupal\entity\EntityInterface $entity, $context) { + // For nodes, change the view mode when it is teaser. + if ($entity->entityType() == 'node' && $view_mode == 'teaser') { + $view_mode = 'my_custom_view_mode'; + } +} diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 1b0d670f5ea1c8da05f50ba5567667968985c10c..349aab53f4af458de0146e998ef150ea275ed68b 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1202,6 +1202,10 @@ function node_build_content(Node $node, $view_mode = 'full', $langcode = NULL) { // Remove previously built content, if exists. $node->content = array(); + // Allow modules to change the view mode. + $context = array('langcode' => $langcode); + drupal_alter('entity_view_mode', $view_mode, $node, $context); + // The 'view' hook can be implemented to overwrite the default function // to display nodes. if (node_hook($node, 'view')) { diff --git a/core/modules/node/tests/modules/node_test/node_test.module b/core/modules/node/tests/modules/node_test/node_test.module index f541d10832eab28ab0a52d0caa156b5ccaaa628c..bfad466649e4d4729ef49c818ae03407c5a5630c 100644 --- a/core/modules/node/tests/modules/node_test/node_test.module +++ b/core/modules/node/tests/modules/node_test/node_test.module @@ -151,3 +151,13 @@ function node_test_node_update(Node $node) { } } } + +/** + * Implements hook_entity_view_mode_alter(). + */ +function node_test_entity_view_mode_alter(&$view_mode, Drupal\entity\EntityInterface $entity, $context) { + // Only alter the view mode if we are on the test callback. + if ($change_view_mode = variable_get('node_test_change_view_mode', '')) { + $view_mode = $change_view_mode; + } +} diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index a37da8aed9a51b359a17fcc9a340733c88255023..a772e0259be4a3d410a46958bc178e9694f1ae49 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -587,6 +587,10 @@ function taxonomy_term_view(Term $term, $view_mode = 'full', $langcode = NULL) { $langcode = language(LANGUAGE_TYPE_CONTENT)->langcode; } + // Allow modules to change the view mode. + $context = array('langcode' => $langcode); + drupal_alter('entity_view_mode', $view_mode, $term, $context); + field_attach_prepare_view('taxonomy_term', array($term->tid => $term), $view_mode, $langcode); entity_prepare_view('taxonomy_term', array($term->tid => $term), $langcode); diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 4217234368442471f46ec4ac5c0e19d44e22230a..44cb2664e1b19da00f6141cd1a140603fe7ac549 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -2115,6 +2115,10 @@ function user_build_content($account, $view_mode = 'full', $langcode = NULL) { // Remove previously built content, if exists. $account->content = array(); + // Allow modules to change the view mode. + $context = array('langcode' => $langcode); + drupal_alter('entity_view_mode', $view_mode, $account, $context); + // Build fields content. field_attach_prepare_view('user', array($account->uid => $account), $view_mode, $langcode); entity_prepare_view('user', array($account->uid => $account), $langcode);