From 435585012c75bd5073d36cf6e7500864c6332bf8 Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Sat, 23 Oct 2010 15:30:34 +0000
Subject: [PATCH] #949576 by sun: Add missing hook_entity_view() and
 hook_entity_view_alter().

---
 modules/comment/comment.api.php   |  3 ++
 modules/comment/comment.module    |  4 +-
 modules/node/node.api.php         |  3 ++
 modules/node/node.module          |  4 +-
 modules/system/system.api.php     | 62 +++++++++++++++++++++++++++++++
 modules/taxonomy/taxonomy.api.php | 28 ++++++++++++++
 modules/taxonomy/taxonomy.module  |  4 ++
 modules/user/user.api.php         |  4 ++
 modules/user/user.module          |  4 +-
 9 files changed, 113 insertions(+), 3 deletions(-)

diff --git a/modules/comment/comment.api.php b/modules/comment/comment.api.php
index d2b03911cb57..987d0467397a 100644
--- a/modules/comment/comment.api.php
+++ b/modules/comment/comment.api.php
@@ -68,6 +68,8 @@ function hook_comment_load($comments) {
  *   View mode, e.g. 'full', 'teaser'...
  * @param $langcode
  *   The language code used for rendering.
+ *
+ * @see hook_entity_view()
  */
 function hook_comment_view($comment, $view_mode, $langcode) {
   // how old is the comment
@@ -90,6 +92,7 @@ function hook_comment_view($comment, $view_mode, $langcode) {
  *   A renderable array representing the comment.
  *
  * @see comment_view()
+ * @see hook_entity_view_alter()
  */
 function hook_comment_view_alter(&$build) {
   // Check for the existence of a field added by another module.
diff --git a/modules/comment/comment.module b/modules/comment/comment.module
index 70789b794239..52549f974eea 100644
--- a/modules/comment/comment.module
+++ b/modules/comment/comment.module
@@ -939,7 +939,8 @@ function comment_view($comment, $node, $view_mode = 'full', $langcode = NULL) {
   }
 
   // Allow modules to modify the structured comment.
-  drupal_alter('comment_view', $build);
+  $type = 'comment';
+  drupal_alter(array('comment_view', 'entity_view'), $build, $type);
 
   return $build;
 }
@@ -983,6 +984,7 @@ function comment_build_content($comment, $node, $view_mode = 'full', $langcode =
 
   // Allow modules to make their own additions to the comment.
   module_invoke_all('comment_view', $comment, $view_mode, $langcode);
+  module_invoke_all('entity_view', $comment, 'comment', $view_mode, $langcode);
 }
 
 /**
diff --git a/modules/node/node.api.php b/modules/node/node.api.php
index 253c2e35485b..386c8002850b 100644
--- a/modules/node/node.api.php
+++ b/modules/node/node.api.php
@@ -759,6 +759,8 @@ function hook_node_submit($node, $form, &$form_state) {
  * @param $langcode
  *   The language code used for rendering.
  *
+ * @see hook_entity_view()
+ *
  * @ingroup node_api_hooks
  */
 function hook_node_view($node, $view_mode, $langcode) {
@@ -785,6 +787,7 @@ function hook_node_view($node, $view_mode, $langcode) {
  *   A renderable array representing the node content.
  *
  * @see node_view()
+ * @see hook_entity_view_alter()
  *
  * @ingroup node_api_hooks
  */
diff --git a/modules/node/node.module b/modules/node/node.module
index cce34c9e3a06..710a3851a25f 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -1270,7 +1270,8 @@ function node_view($node, $view_mode = 'full', $langcode = NULL) {
   }
 
   // Allow modules to modify the structured node.
-  drupal_alter('node_view', $build);
+  $type = 'node';
+  drupal_alter(array('node_view', 'entity_view'), $build, $type);
 
   return $build;
 }
@@ -1343,6 +1344,7 @@ function node_build_content($node, $view_mode = 'full', $langcode = NULL) {
 
   // Allow modules to make their own additions to the node.
   module_invoke_all('node_view', $node, $view_mode, $langcode);
+  module_invoke_all('entity_view', $node, 'node', $view_mode, $langcode);
 }
 
 /**
diff --git a/modules/system/system.api.php b/modules/system/system.api.php
index 20ddc4f62a94..c68d893d1622 100644
--- a/modules/system/system.api.php
+++ b/modules/system/system.api.php
@@ -355,6 +355,68 @@ function hook_entity_query_alter($query) {
   $query->executeCallback = 'my_module_query_callback';
 }
 
+/**
+ * Act on entities being assembled before rendering.
+ *
+ * @param $entity
+ *   The entity object.
+ * @param $type
+ *   The type of entity being rendered (i.e. node, user, comment).
+ * @param $view_mode
+ *   The view mode the entity is rendered in.
+ * @param $langcode
+ *   The language code used for rendering.
+ *
+ * The module may add elements to $entity->content prior to rendering. The
+ * structure of $entity->content is a renderable array as expected by
+ * drupal_render().
+ *
+ * @see hook_entity_view_alter()
+ * @see hook_comment_view()
+ * @see hook_node_view()
+ * @see hook_user_view()
+ */
+function hook_entity_view($entity, $type, $view_mode, $langcode) {
+  $entity->content['my_additional_field'] = array(
+    '#markup' => $additional_field,
+    '#weight' => 10,
+    '#theme' => 'mymodule_my_additional_field',
+  );
+}
+
+/**
+ * Alter the results of ENTITY_view().
+ *
+ * This hook is called after the content has been assembled in a structured
+ * array and may be used for doing processing which requires that the complete
+ * entity content structure has been built.
+ *
+ * If a module wishes to act on the rendered HTML of the entity rather than the
+ * structured content array, it may use this hook to add a #post_render
+ * callback. Alternatively, it could also implement hook_preprocess_ENTITY().
+ * See drupal_render() and theme() for details.
+ *
+ * @param $build
+ *   A renderable array representing the entity content.
+ * @param $type
+ *   The type of entity being rendered (i.e. node, user, comment).
+ *
+ * @see hook_entity_view()
+ * @see hook_comment_view_alter()
+ * @see hook_node_view_alter()
+ * @see hook_taxonomy_term_view_alter()
+ * @see hook_user_view_alter()
+ */
+function hook_entity_view_alter(&$build, $type) {
+  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
+    // Change its weight.
+    $build['an_additional_field']['#weight'] = -10;
+
+    // Add a #post_render callback to act on the rendered HTML of the entity.
+    $build['#post_render'][] = 'my_module_node_post_render';
+  }
+}
+
 /**
  * Define administrative paths.
  *
diff --git a/modules/taxonomy/taxonomy.api.php b/modules/taxonomy/taxonomy.api.php
index 6ceab8f80ae9..1527e487c417 100644
--- a/modules/taxonomy/taxonomy.api.php
+++ b/modules/taxonomy/taxonomy.api.php
@@ -182,6 +182,34 @@ function hook_taxonomy_term_delete($term) {
   db_delete('term_synoynm')->condition('tid', $term->tid)->execute();
 }
 
+/**
+ * Alter the results of taxonomy_term_view().
+ *
+ * This hook is called after the content has been assembled in a structured
+ * array and may be used for doing processing which requires that the complete
+ * taxonomy term content structure has been built.
+ *
+ * If the module wishes to act on the rendered HTML of the term rather than the
+ * structured content array, it may use this hook to add a #post_render
+ * callback. Alternatively, it could also implement
+ * hook_preprocess_taxonomy_term(). See drupal_render() and theme()
+ * documentation respectively for details.
+ *
+ * @param $build
+ *   A renderable array representing the node content.
+ *
+ * @see hook_entity_view_alter()
+ */
+function hook_taxonomy_term_view_alter(&$build) {
+  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
+    // Change its weight.
+    $build['an_additional_field']['#weight'] = -10;
+  }
+
+  // Add a #post_render callback to act on the rendered HTML of the term.
+  $build['#post_render'][] = 'my_module_node_post_render';
+}
+
 /**
  * @} End of "addtogroup hooks".
  */
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index b36bca9c378a..1e0cf45ad5bb 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -665,6 +665,10 @@ function taxonomy_term_view($term, $view_mode = 'full', $langcode = NULL) {
 
   $build['#attached']['css'][] = drupal_get_path('module', 'taxonomy') . '/taxonomy.css';
 
+  // Allow modules to modify the structured term.
+  $type = 'taxonomy_term';
+  drupal_alter(array('taxonomy_term_view', 'entity_view'), $build, $type);
+
   return $build;
 }
 
diff --git a/modules/user/user.api.php b/modules/user/user.api.php
index d6723db14895..722fb31dc540 100644
--- a/modules/user/user.api.php
+++ b/modules/user/user.api.php
@@ -316,6 +316,9 @@ function hook_user_logout($account) {
  *   View mode, e.g. 'full'.
  * @param $langcode
  *   The language code used for rendering.
+ *
+ * @see hook_user_view_alter()
+ * @see hook_entity_view()
  */
 function hook_user_view($account, $view_mode, $langcode) {
   if (user_access('create blog content', $account)) {
@@ -344,6 +347,7 @@ function hook_user_view($account, $view_mode, $langcode) {
  *   A renderable array representing the user.
  *
  * @see user_view()
+ * @see hook_entity_view_alter()
  */
 function hook_user_view_alter(&$build) {
   // Check for the existence of a field added by another module.
diff --git a/modules/user/user.module b/modules/user/user.module
index adfb493c1a28..c0372e658813 100644
--- a/modules/user/user.module
+++ b/modules/user/user.module
@@ -2452,7 +2452,8 @@ function user_view($account, $view_mode = 'full', $langcode = NULL) {
   );
 
   // Allow modules to modify the structured user.
-  drupal_alter('user_view', $build);
+  $type = 'user';
+  drupal_alter(array('user_view', 'entity_view'), $build, $type);
 
   return $build;
 }
@@ -2483,6 +2484,7 @@ function user_build_content($account, $view_mode = 'full', $langcode = NULL) {
 
   // Populate $account->content with a render() array.
   module_invoke_all('user_view', $account, $view_mode, $langcode);
+  module_invoke_all('entity_view', $account, 'user', $view_mode, $langcode);
 }
 
 /**
-- 
GitLab