1 entity.module entity_view_mode_prepare($entity_type, $entities, $view_mode, $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 display 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.


$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 display 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 display mode.:

See also



core/modules/entity/entity.module, line 714
Entity API for handling entities like nodes or users.


function entity_view_mode_prepare($entity_type, $entities, $view_mode, $langcode = NULL) {
  if (!isset($langcode)) {
    global $language_content;
    $langcode = $language_content->langcode;

  // 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 display mode.
      $context = array(
        'entity_type' => $entity_type,
        'entity' => $entity,
        'langcode' => $langcode,
      backdrop_alter('entity_view_mode', $entity_view_mode, $context);

    $entities_by_view_mode[$entity_view_mode][$id] = $entity;

  return $entities_by_view_mode;