diff --git a/includes/common.inc b/includes/common.inc index 0ebe857..302a5c0 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -7814,6 +7814,55 @@ function entity_prepare_view($entity_type, $entities, $langcode = NULL) { } /** + * Invoke hook_entity_view_mode_alter(). + * + * If adding a new entity similar to nodes, comments or users, you should + * invoke this function during the ENTITY_build_content() or + * ENTITY_view_multiple() phases of rendering to allow other modules to alter + * the view mode during this phase. This function needs to be called before + * field_attach_prepare_view() to ensure that the correct content is + * loaded by field API. + * + * @param $entity_type + * The type of entity, i.e. 'node', 'user'. + * @param $entities + * The entity objects which are being prepared for view, keyed by object ID. + * @param $langcode + * (optional) A language code to be used for rendering. Defaults to the global + * content language of the current request. + * @return + * An associative array with arrays of entities keyed by view mode. + * + * @see hook_entity_view_mode_alter() + */ +function entity_view_mode_prepare($entity_type, $entities, $view_mode, $langcode = NULL) { + if (!isset($langcode)) { + $langcode = $GLOBALS['language_content']->language; + } + + // To ensure hooks are never run after field_attach_prepare_view() + // only process items without the entity_view_prepared flag. + $entities_by_view_mode = array(); + foreach ($entities as $id => $entity) { + $entity_view_mode = $view_mode; + if (empty($entity->entity_view_prepared)) { + + // Allow modules to change the view mode. + $context = array( + 'entity_type' => $entity_type, + 'entity' => $entity, + 'langcode' => $langcode, + ); + drupal_alter('entity_view_mode', $entity_view_mode, $context); + } + + $entities_by_view_mode[$entity_view_mode][$entity->nid] = $entity; + } + + return $entities_by_view_mode; +} + +/** * Returns the URI elements of an entity. * * @param $entity_type diff --git a/modules/comment/comment.module b/modules/comment/comment.module index 46115be..1ff71b0 100644 --- a/modules/comment/comment.module +++ b/modules/comment/comment.module @@ -993,12 +993,7 @@ function comment_build_content($comment, $node, $view_mode = 'full', $langcode = $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); + $view_mode = key(entity_view_mode_prepare('comment', array($comment->cid => $comment), $view_mode, $langcode)); // Build fields content. field_attach_prepare_view('comment', array($comment->cid => $comment), $view_mode, $langcode); @@ -1108,8 +1103,11 @@ function comment_links($comment, $node) { * An array in the format expected by drupal_render(). */ function comment_view_multiple($comments, $node, $view_mode = 'full', $weight = 0, $langcode = NULL) { - field_attach_prepare_view('comment', $comments, $view_mode, $langcode); - entity_prepare_view('comment', $comments, $langcode); + $comments_by_view_mode = entity_view_mode_prepare('comment', $comments, $view_mode, $langcode); + foreach ($comments_by_view_mode as $view_mode => $comments) { + field_attach_prepare_view('comment', $comments, $view_mode, $langcode); + entity_prepare_view('comment', $comments, $langcode); + } $build = array( '#sorted' => TRUE, diff --git a/modules/node/node.module b/modules/node/node.module index 04911de..4e37bf1 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -1373,12 +1373,7 @@ function node_build_content($node, $view_mode = 'full', $langcode = NULL) { $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); + $view_mode = key(entity_view_mode_prepare('node', array($node->nid => $node), $view_mode, $langcode)); // The 'view' hook can be implemented to overwrite the default function // to display nodes. @@ -2637,8 +2632,12 @@ function node_feed($nids = FALSE, $channel = array()) { * An array in the format expected by drupal_render(). */ function node_view_multiple($nodes, $view_mode = 'teaser', $weight = 0, $langcode = NULL) { - field_attach_prepare_view('node', $nodes, $view_mode, $langcode); - entity_prepare_view('node', $nodes, $langcode); + $nodes_by_view_mode = entity_view_mode_prepare('node', $nodes, $view_mode, $langcode); + foreach ($nodes_by_view_mode as $view_mode => $nodes) { + field_attach_prepare_view('node', $nodes, $view_mode, $langcode); + entity_prepare_view('node', $nodes, $langcode); + } + $build = array(); foreach ($nodes as $node) { $build['nodes'][$node->nid] = node_view($node, $view_mode, $langcode); diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index 9be7dfc..4a84485 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -771,8 +771,11 @@ function taxonomy_term_show($term) { * An array in the format expected by drupal_render(). */ function taxonomy_term_view_multiple($terms, $view_mode = 'teaser', $weight = 0, $langcode = NULL) { - field_attach_prepare_view('taxonomy_term', $terms, $view_mode, $langcode); - entity_prepare_view('taxonomy_term', $terms, $langcode); + $terms_by_view_mode = entity_view_mode_prepare('taxonomy_term', $terms, $view_mode, $langcode); + foreach ($terms_by_view_mode as $view_mode => $terms) { + field_attach_prepare_view('taxonomy_term', $terms, $view_mode, $langcode); + entity_prepare_view('taxonomy_term', $terms, $langcode); + } $build = array(); foreach ($terms as $term) { $build['taxonomy_terms'][$term->tid] = taxonomy_term_view($term, $view_mode, $langcode); @@ -812,12 +815,7 @@ function taxonomy_term_build_content($term, $view_mode = 'full', $langcode = NUL $term->content = array(); // 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); + $view_mode = key(entity_view_mode_prepare('taxonomy_term', array($term->tid => $term), $view_mode, $langcode)); // Add the term description if the term has one and it is visible. $type = 'taxonomy_term'; diff --git a/modules/user/user.module b/modules/user/user.module index c1c7ec2..28afde3 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -2596,12 +2596,7 @@ function user_build_content($account, $view_mode = 'full', $langcode = NULL) { $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); + $view_mode = key(entity_view_mode_prepare('user', array($account->uid => $account), $view_mode, $langcode)); // Build fields content. field_attach_prepare_view('user', array($account->uid => $account), $view_mode, $langcode);