diff --git a/modules/comment/comment.module b/modules/comment/comment.module index 429c3b0..9820568 100644 --- a/modules/comment/comment.module +++ b/modules/comment/comment.module @@ -992,6 +992,14 @@ function comment_build_content($comment, $node, $view_mode = 'full', $langcode = // 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/modules/node/node.module b/modules/node/node.module index 2baee43..e3e3f9e 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -1345,6 +1345,14 @@ function node_build_content($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/modules/node/node.test b/modules/node/node.test index 7080ce7..3c9fbaa 100644 --- a/modules/node/node.test +++ b/modules/node/node.test @@ -2597,3 +2597,48 @@ class NodeAccessFieldTestCase extends NodeWebTestCase { $this->assertRaw($default, 'The updated default value is displayed when creating a new node.'); } } + +/** + * Tests changing view modes for nodes. + */ +class NodeEntityViewModeAlterTest extends NodeWebTestCase { + + public static function getInfo() { + return array( + 'name' => 'Node entity view mode', + 'description' => 'Test changing view mode.', + 'group' => 'Node' + ); + } + + function setUp() { + parent::setUp(array('node_test')); + } + + /** + * Create a "Basic page" node and verify its consistency in the database. + */ + function testNodeViewModeChange() { + $web_user = $this->drupalCreateUser(array('create page content', 'edit own page content')); + $this->drupalLogin($web_user); + + // Create a node. + $edit = array(); + $langcode = LANGUAGE_NONE; + $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/modules/node/tests/node_test.module b/modules/node/tests/node_test.module index b0ebc14..a52c1fa 100644 --- a/modules/node/tests/node_test.module +++ b/modules/node/tests/node_test.module @@ -149,3 +149,13 @@ function node_test_node_update($node) { } } } + +/** + * Implements hook_entity_view_mode_alter(). + */ +function node_test_entity_view_mode_alter(&$view_mode, $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/modules/system/system.api.php b/modules/system/system.api.php index fbb0a55..c51fc44 100644 --- a/modules/system/system.api.php +++ b/modules/system/system.api.php @@ -466,6 +466,24 @@ function hook_entity_view_alter(&$build, $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 + * Array with contextual information, including: + * - entity_type: The type of the entity that is being viewed. + * - entity: The entity object. + * - langcode: The langcode the entity is being viewed in. + */ +function hook_entity_view_mode_alter(&$view_mode, $context) { + // For nodes, change the view mode when it is teaser. + if ($context['entity_type'] == 'node' && $view_mode == 'teaser') { + $view_mode = 'my_custom_view_mode'; + } +} + +/** * Define administrative paths. * * Modules may specify whether or not the paths they define in hook_menu() are @@ -1041,7 +1059,7 @@ function hook_menu_get_item_alter(&$router_item, $path, $original_map) { * This 'abc' object will then be passed into the callback functions defined * for the menu item, such as the page callback function mymodule_abc_edit() * to replace the integer 1 in the argument array. Note that a load function - * should return FALSE when it is unable to provide a loadable object. For + * should return FALSE when it is unable to provide a loadable object. For * example, the node_load() function for the 'node/%node/edit' menu item will * return FALSE for the path 'node/999/edit' if a node with a node ID of 999 * does not exist. The menu routing system will return a 404 error in this case. diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index df4084c..c86c510 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -754,6 +754,14 @@ function taxonomy_term_view($term, $view_mode = 'full', $langcode = NULL) { $langcode = $GLOBALS['language_content']->language; } + // 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/modules/user/user.module b/modules/user/user.module index 47ac642..bf3f3b1 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -2574,6 +2574,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);