summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.txt2
-rw-r--r--modules/comment/comment.module8
-rw-r--r--modules/node/node.module8
-rw-r--r--modules/node/node.test45
-rw-r--r--modules/node/tests/node_test.module10
-rw-r--r--modules/system/system.api.php18
-rw-r--r--modules/taxonomy/taxonomy.module8
-rw-r--r--modules/user/user.module8
8 files changed, 107 insertions, 0 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 6f1ca3a3c..b276327b0 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -18,6 +18,8 @@ Drupal 7.16, xxxx-xx-xx (development version)
projects.
- Fixed a regression which caused a "call to undefined function
drupal_find_base_themes()" fatal error under rare circumstances.
+- Added hook_entity_view_mode_alter() to allow modules to change entity view
+ modes on display.
Drupal 7.15, 2012-08-01
-----------------------
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);