diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index dfa156d..e62b710 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1030,6 +1030,9 @@ 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. + drupal_alter('entity_view_mode', $view_mode, $comment, $langcode); + // 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..c47ba55 100644 --- a/core/modules/entity/entity.api.php +++ b/core/modules/entity/entity.api.php @@ -434,3 +434,20 @@ 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 string $langcode + * The language code the entity is being displayed for. + */ +function hook_entity_view_mode_alter(&$view_mode, Drupal\entity\EntityInterface $entity, $langcode) { + // 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/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 3d83cd4..f7ccdbf 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1272,6 +1272,9 @@ 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. + drupal_alter('entity_view_mode', $view_mode, $node, $langcode); + // 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..1b301c3 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, $langcode) { + // 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 8aa78df..43374f3 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -588,6 +588,9 @@ 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. + drupal_alter('entity_view_mode', $view_mode, $term, $langcode); + 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 e373062..591fca1 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -2415,6 +2415,9 @@ 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. + drupal_alter('entity_view_mode', $view_mode, $account, $langcode); + // Build fields content. field_attach_prepare_view('user', array($account->uid => $account), $view_mode, $langcode); entity_prepare_view('user', array($account->uid => $account), $langcode);