You are here

function entity_prepare_view in Drupal 7

Invoke hook_entity_prepare_view().

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 objects during this phase. This is needed for situations where information needs to be loaded outside of ENTITY_load() - particularly when loading entities into one another - i.e. a user object into a node, due to the potential for unwanted side-effects such as caching and infinite recursion. By convention, entity_prepare_view() is called after field_attach_prepare_view() to allow entity level hooks to act on content loaded by field API.

Parameters

$entity_type: The type of entity, i.e. 'node', 'user'.

$entities: The entity objects which are being prepared for view, keyed by object ID.

$langcode: (optional) A language code to be used for rendering. Defaults to the global content language of the current request.

See also

hook_entity_prepare_view()

7 calls to entity_prepare_view()
comment_build_content in modules/comment/comment.module
Builds a structured array representing the comment's content.
comment_view_multiple in modules/comment/comment.module
Construct a drupal_render() style array from an array of loaded comments.
node_build_content in modules/node/node.module
Builds a structured array representing the node's content.
node_view_multiple in modules/node/node.module
Constructs a drupal_render() style array from an array of loaded nodes.
taxonomy_term_build_content in modules/taxonomy/taxonomy.module
Builds a structured array representing the term's content.

... See full list

File

includes/common.inc, line 8189
Common functions that many Drupal modules will need to reference.

Code

function entity_prepare_view($entity_type, $entities, $langcode = NULL) {
  if (!isset($langcode)) {
    $langcode = $GLOBALS['language_content']->language;
  }

  // To ensure hooks are only run once per entity, check for an
  // entity_view_prepared flag and only process items without it.
  // @todo: resolve this more generally for both entity and field level hooks.
  $prepare = array();
  foreach ($entities as $id => $entity) {
    if (empty($entity->entity_view_prepared)) {

      // Add this entity to the items to be prepared.
      $prepare[$id] = $entity;

      // Mark this item as prepared.
      $entity->entity_view_prepared = TRUE;
    }
  }
  if (!empty($prepare)) {
    module_invoke_all('entity_prepare_view', $prepare, $entity_type, $langcode);
  }
}