commit 4213a18a6a0cb8d0a86eeba02f38604579e4a618
Author: Damien Tournoud <damien@commerceguys.com>
Date:   Mon Jul 4 11:49:15 2011 +0200

    Issue #1208864 option 1: allow the caller to set the 'page' flag.

diff --git a/entity.module b/entity.module
index 7ec5f13..3765cbc 100644
--- a/entity.module
+++ b/entity.module
@@ -369,17 +369,19 @@ function entity_id($entity_type, $entity) {
  * @param $langcode
  *   (optional) A language code to use for rendering. Defaults to the global
  *   content language of the current request.
+ * @param $page
+ *   (optional) If TRUE, the entity will be rendered in page mode.
  * @return
  *   The renderable array, keyed by name key if existing or by numeric id else.
  *   If there is no information on how to view an entity, FALSE is returned.
  */
-function entity_view($entity_type, $entities, $view_mode = 'full', $langcode = NULL) {
+function entity_view($entity_type, $entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
   $info = entity_get_info($entity_type);
   if (isset($info['view callback'])) {
-    return $info['view callback']($entities, $view_mode, $langcode, $entity_type);
+    return $info['view callback']($entities, $view_mode, $langcode, $entity_type, $page);
   }
   elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
-    return entity_get_controller($entity_type)->view($entities, $view_mode, $langcode);
+    return entity_get_controller($entity_type)->view($entities, $view_mode, $langcode, $page);
   }
   return FALSE;
 }
@@ -801,9 +803,16 @@ function template_preprocess_entity(&$variables) {
   $info = entity_get_info($entity_type);
 
   $variables['title'] = check_plain(entity_label($entity_type, $entity));
-  $uri = entity_uri($entity_type, $entity);
-  $variables['url'] = $uri ? url($uri['path'], $uri['options']) : FALSE;
-  $variables['page'] = $uri && $uri['path'] == $_GET['q'];
+  if (isset($variables['elements']['#page'])) {
+    // If set by the caller, respect the page property.
+    $variables['page'] = $variables['elements']['#page'];
+  }
+  else {
+    // Else, try to automatically detect it.
+    $uri = entity_uri($entity_type, $entity);
+    $variables['url'] = $uri ? url($uri['path'], $uri['options']) : FALSE;
+    $variables['page'] = $uri && $uri['path'] == $_GET['q'];
+  }
 
   // Helpful $content variable for templates.
   $variables['content'] = array();
diff --git a/includes/entity.controller.inc b/includes/entity.controller.inc
index 7244c38..b86201e 100644
--- a/includes/entity.controller.inc
+++ b/includes/entity.controller.inc
@@ -105,10 +105,12 @@ interface EntityAPIControllerInterface extends DrupalEntityControllerInterface {
    * @param $langcode
    *   (optional) A language code to use for rendering. Defaults to the global
    *   content language of the current request.
+   * @param $page
+   *   (optional) If TRUE, the entity will be rendered in page mode.
    * @return
    *   The renderable array, keyed by entity name or numeric id.
    */
-  public function view($entities, $view_mode = 'full', $langcode = NULL);
+  public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL);
 }
 
 /**
@@ -439,7 +441,7 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
   /**
    * Implements EntityAPIControllerInterface.
    */
-  public function view($entities, $view_mode = 'full', $langcode = NULL) {
+  public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
     // For Field API and entity_prepare_view, the entities have to be keyed by
     // (numeric) id.
     $entities = entity_key_array_by_property($entities, $this->idKey);
@@ -461,6 +463,7 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
         '#entity' => $entity,
         '#view_mode' => $view_mode,
         '#language' => $langcode,
+        '#page' => $page,
       );
       // Allow modules to modify the structured entity.
       drupal_alter(array($this->entityType . '_view', 'entity_view'), $build, $this->entityType);
@@ -709,8 +712,8 @@ class EntityAPIControllerExportable extends EntityAPIController {
   /**
    * Implements EntityAPIControllerInterface.
    */
-  public function view($entities, $view_mode = 'full', $langcode = NULL) {
-    $view = parent::view($entities, $view_mode, $langcode);
+  public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
+    $view = parent::view($entities, $view_mode, $langcode, $page);
 
     // Re-key the view array to be keyed by name.
     $key = isset($entity->{$this->nameKey}) ? $entity->{$this->nameKey} : NULL;
