You are here

function user_reference_field_formatter_prepare_view in References 7.2

Implements hook_field_formatter_prepare_view().

Preload all user referenced by items using 'full entity' formatters.

File

user_reference/user_reference.module, line 383
Defines a field type for referencing a user from a node.

Code

function user_reference_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {

  // Load the referenced users, except for the 'user_reference_uid' which does
  // not need full objects.
  // Collect ids to load.
  $ids = array();
  foreach ($displays as $id => $display) {
    if ($display['type'] != 'user_reference_uid') {
      foreach ($items[$id] as $delta => $item) {
        if ($item['access']) {
          $ids[$item['uid']] = $item['uid'];
        }
      }
    }
  }
  $entities = user_load_multiple($ids);

  // Add the loaded user objects to the items.
  foreach ($displays as $id => $display) {
    if ($display['type'] != 'user_reference_uid') {
      foreach ($items[$id] as $delta => $item) {
        if ($item['access']) {
          $items[$id][$delta]['user'] = $entities[$item['uid']];
        }
      }
    }
  }
}