You are here

function entity_view_mode_prepare in Drupal 7

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.

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.

$view_mode: The original view mode e.g. 'full', 'teaser'...

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

Return value

An associative array with arrays of entities keyed by view mode.

See also

hook_entity_view_mode_alter()

7 calls to entity_view_mode_prepare()
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 8236
Common functions that many Drupal modules will need to reference.

Code

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][$id] = $entity;
  }
  return $entities_by_view_mode;
}