diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index a6230f2..93de992 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1023,6 +1023,14 @@ 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( + 'entity_type' => 'comment', + 'entity' => $comment, + 'langcode' => $langcode, + ); + drupal_alter('entity_view_mode', $view_mode, $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 02d8754..48ba114 100644 --- a/core/modules/entity/entity.api.php +++ b/core/modules/entity/entity.api.php @@ -434,3 +434,15 @@ 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 array $context + * An array with the keys entity_type, entity and langcode. + */ +function hook_entity_view_mode_alter(&$view_mode, array $context) { + +} \ No newline at end of file diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeEntityViewModeAlterTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeEntityViewModeAlterTest.php new file mode 100644 index 0000000..fba3ffe --- /dev/null +++ b/core/modules/node/lib/Drupal/node/Tests/NodeEntityViewModeAlterTest.php @@ -0,0 +1,53 @@ + 'Node entity view mode', + 'description' => 'Test changing view mode.', + 'group' => 'Node' + ); + } + + function setUp() { + // Enable dummy module that implements hook_node_view(). + parent::setUp('node_test'); + + $web_user = $this->drupalCreateUser(array('create page content', 'edit own page content')); + $this->drupalLogin($web_user); + } + + /** + * Create a "Basic page" node and verify its consistency in the database. + */ + function testNodeViewModeChange() { + // Create a node. + $edit = array(); + $langcode = LANGUAGE_NOT_SPECIFIED; + $edit["title"] = $this->randomName(8); + $edit["body[$langcode][0][value]"] = t('Data that should appear only in the body for the node.'); + $edit["body[$langcode][0][summary]"] = t('Extra data that should appear only in the teaser for the node.'); + $this->drupalPost('node/add/page', $edit, t('Save')); + + $node = $this->drupalGetNodeByTitle($edit["title"]); + + // Set the flag to alter the view mode and view the node. + variable_set('node_test_change_view_mode', 'teaser'); + $this->drupalGet('node/' . $node->nid); + + // Check that teaser mode is viewed. + $this->assertText('Extra data that should appear only in the teaser for the node.', 'Teaser text present'); + // Make sure body text is not present. + $this->assertNoText('Data that should appear only in the body for the node.', 'Body text not present'); + } +} diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 49d20a7..c3c7d68 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1246,6 +1246,14 @@ 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( + 'entity_type' => 'node', + 'entity' => $node, + 'langcode' => $langcode, + ); + drupal_alter('entity_view_mode', $view_mode, $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 f541d10..6a5520e 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, array $context) { + // Only alter the view mode if we are on the test callback. + if ($view_mode = variable_get('node_test_change_view_mode', '')) { + $view_mode = $view_mode; + } +} diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 6697a0a..2d65e10 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -587,6 +587,14 @@ function taxonomy_term_view(Term $term, $view_mode = 'full', $langcode = NULL) { $langcode = drupal_container()->get(LANGUAGE_TYPE_CONTENT)->langcode; } + // Allow modules to change the view mode. + $context = array( + 'entity_type' => 'taxonomy_term', + 'entity' => $term, + 'langcode' => $langcode, + ); + drupal_alter('entity_view_mode', $view_mode, $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 89706c4..552dde9 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -2405,6 +2405,14 @@ 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( + 'entity_type' => 'user', + 'entity' => $account, + 'langcode' => $langcode, + ); + drupal_alter('entity_view_mode', $view_mode, $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);