You are here

function node_reference_field_formatter_prepare_view in References 7.2

Implements hook_field_formatter_prepare_view().

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

File

node_reference/node_reference.module, line 403
Defines a field type for referencing one node from another.

Code

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

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

        // Checking item 'access' before the use.
        if (!empty($item['access'])) {
          $ids[$item['nid']] = $item['nid'];
        }
      }
    }
  }
  $entities = node_load_multiple($ids);

  // Add the loaded nodes to the items.
  foreach ($displays as $id => $display) {
    if ($display['type'] != 'node_reference_nid') {
      foreach ($items[$id] as $delta => $item) {

        // Checking item 'access' before the use.
        if (!empty($item['access'])) {
          $items[$id][$delta]['node'] = $entities[$item['nid']];
        }
      }
    }
  }
}