diff options
author | webchick <webchick@24967.no-reply.drupal.org> | 2012-09-30 20:37:55 -0400 |
---|---|---|
committer | webchick <webchick@24967.no-reply.drupal.org> | 2012-09-30 20:37:55 -0400 |
commit | 13f16ae3423087824118e1e5c07f88396a85c8f7 (patch) | |
tree | 7a9678d7256d44fdcd4d5580a548ffca98aa7459 /modules | |
parent | c3cba0532ba45692af5378d1b89e745ea0df3701 (diff) | |
download | brdo-13f16ae3423087824118e1e5c07f88396a85c8f7.tar.gz brdo-13f16ae3423087824118e1e5c07f88396a85c8f7.tar.bz2 |
Issue #1154382 by Berdir, barraponto, acouch, swentel, LoMo, DamienMcKenna, chx: Fixed View mode no longer can be changed.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/comment/comment.module | 8 | ||||
-rw-r--r-- | modules/node/node.module | 8 | ||||
-rw-r--r-- | modules/node/node.test | 45 | ||||
-rw-r--r-- | modules/node/tests/node_test.module | 10 | ||||
-rw-r--r-- | modules/system/system.api.php | 18 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.module | 8 | ||||
-rw-r--r-- | modules/user/user.module | 8 |
7 files changed, 105 insertions, 0 deletions
diff --git a/modules/comment/comment.module b/modules/comment/comment.module index 0f2c15a90..80c23e810 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 d3d1c85b1..f181b52bb 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 253e7da84..9ef4c2586 100644 --- a/modules/node/node.test +++ b/modules/node/node.test @@ -2601,3 +2601,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 b0ebc149a..a52c1fad0 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 dda62f7ac..7bf637ec0 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 diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index 7814410c3..07d01d399 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -761,6 +761,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 393ccc8b4..72880f7ae 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); |