summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2012-08-23 00:35:56 (GMT)
committerwebchick2012-08-23 00:35:56 (GMT)
commit92548664d0cf2514f6b767c0df77c2c60bce92cc (patch)
tree6dc9ba12f37c0f64a58f299b2958e3ef45c39c52
parente6cfbba9548670b0b7a63cdd9d43536a0a7f2130 (diff)
Issue #1154382 by Berdir, barraponto, acouch, swentel, LoMo, chx: Fixed View mode no longer can be changed.
-rw-r--r--core/modules/comment/comment.module4
-rw-r--r--core/modules/entity/entity.api.php18
-rw-r--r--core/modules/node/node.module4
-rw-r--r--core/modules/node/tests/modules/node_test/node_test.module10
-rw-r--r--core/modules/taxonomy/taxonomy.module4
-rw-r--r--core/modules/user/user.module4
6 files changed, 44 insertions, 0 deletions
diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 5db5442..aef6209 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -1050,6 +1050,10 @@ 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('langcode' => $langcode);
+ drupal_alter('entity_view_mode', $view_mode, $comment, $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 98e32b8..bd5d147 100644
--- a/core/modules/entity/entity.api.php
+++ b/core/modules/entity/entity.api.php
@@ -448,3 +448,21 @@ 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 array $context
+ * Array with additional context information, currently only contains the
+ * langcode the entity is viewed in.
+ */
+function hook_entity_view_mode_alter(&$view_mode, Drupal\entity\EntityInterface $entity, $context) {
+ // 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/node.module b/core/modules/node/node.module
index 1b0d670..349aab5 100644
--- a/core/modules/node/node.module
+++ b/core/modules/node/node.module
@@ -1202,6 +1202,10 @@ 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('langcode' => $langcode);
+ drupal_alter('entity_view_mode', $view_mode, $node, $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..bfad466 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, $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/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module
index a37da8a..a772e02 100644
--- a/core/modules/taxonomy/taxonomy.module
+++ b/core/modules/taxonomy/taxonomy.module
@@ -587,6 +587,10 @@ function taxonomy_term_view(Term $term, $view_mode = 'full', $langcode = NULL) {
$langcode = language(LANGUAGE_TYPE_CONTENT)->langcode;
}
+ // Allow modules to change the view mode.
+ $context = array('langcode' => $langcode);
+ drupal_alter('entity_view_mode', $view_mode, $term, $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 4217234..44cb266 100644
--- a/core/modules/user/user.module
+++ b/core/modules/user/user.module
@@ -2115,6 +2115,10 @@ 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('langcode' => $langcode);
+ drupal_alter('entity_view_mode', $view_mode, $account, $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);