You are here

function media_field_prepare_view in D7 Media 7

Implements hook_field_prepare_view().

File

includes/media.fields.inc, line 148
: Provides a "Multimedia asset" field to the fields API

Code

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

  // Collect all file IDs that need loading.
  $fids = array();
  foreach ($entities as $id => $entity) {

    // Load the files from the files table.
    foreach ($items[$id] as $delta => $item) {
      if (!empty($item['fid'])) {
        $fids[] = $item['fid'];
      }
    }
  }

  // Load the file entities.
  $files = file_load_multiple($fids);

  // Add the loaded file entities to the field item array.
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => $item) {

      // If the file does not exist, mark the entire item as empty.
      if (empty($files[$item['fid']])) {
        unset($items[$id][$delta]);
      }
      else {
        $items[$id][$delta]['file'] = $files[$item['fid']];
      }
    }
  }
}