You are here

function video_field_load in Video 7.2

Same name and namespace in other branches
  1. 7 video.field.inc \video_field_load()

Implements hook_field_load().

File

./video.field.inc, line 260
Implement a video field, based on the file module's file field.

Code

function video_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  file_field_load($entity_type, $entities, $field, $instances, $langcode, $items, $age);

  // Load all files we need for these entities
  $videofids = array();
  $fids = array();
  foreach ($entities as $id => $entity) {

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

  // Load the queues
  $queues = db_select('video_queue', 'q')
    ->fields('q')
    ->condition('q.fid', $videofids, 'IN')
    ->execute()
    ->fetchAllAssoc('fid');

  // Load the derived files
  $outputs = db_select('video_output', 'vo')
    ->fields('vo')
    ->condition('vo.original_fid', $videofids, 'IN')
    ->execute()
    ->fetchAllAssoc('output_fid');
  $fids = array_merge($fids, array_keys($outputs));
  $files = file_load_multiple($fids);

  // Apply the found information to all files for all entities
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => $item) {
      if (empty($item) || empty($item['fid'])) {
        continue;
      }

      // Check whether transcoding is enabled for this file
      if (isset($queues[$item['fid']])) {
        $items[$id][$delta]['fid'] = intval($item['fid']);
        $items[$id][$delta]['timestamp'] = intval($item['timestamp']);
        $items[$id][$delta]['filesize'] = intval($item['filesize']);
        $items[$id][$delta]['uid'] = intval($item['uid']);
        $items[$id][$delta]['thumbnail'] = intval($item['thumbnail']);
        $items[$id][$delta]['status'] = intval($item['status']);
        $items[$id][$delta]['autoconversion'] = TRUE;
        $items[$id][$delta]['conversioncompleted'] = $queues[$item['fid']]->status == VIDEO_RENDERING_COMPLETE;
        $items[$id][$delta]['conversionstatus'] = intval($queues[$item['fid']]->status);
        $items[$id][$delta]['duration'] = intval($queues[$item['fid']]->duration);

        // Load converted files
        $items[$id][$delta]['playablefiles'] = array();
        foreach ($outputs as $outputfid => $output) {
          if ($output->original_fid == $items[$id][$delta]['fid'] && isset($files[$outputfid])) {
            $conv = (array) $files[$outputfid] + (array) $output;
            $items[$id][$delta]['playablefiles'][] = (object) $conv;
          }
        }

        // If for some reason there are no playable files, mark the status as failed
        if (empty($items[$id][$delta]['playablefiles']) && $items[$id][$delta]['conversioncompleted']) {
          $items[$id][$delta]['conversioncompleted'] = FALSE;
          $items[$id][$delta]['conversionstatus'] = VIDEO_RENDERING_FAILED;
        }
      }
      else {
        $items[$id][$delta]['playablefiles'] = array(
          (object) $items[$id][$delta],
        );
        $items[$id][$delta]['autoconversion'] = FALSE;
        $items[$id][$delta]['duration'] = NULL;
      }

      // Load thumbnail
      if (empty($item['thumbnail']) || !isset($files[$item['thumbnail']])) {
        $items[$id][$delta]['thumbnail'] = NULL;
        $items[$id][$delta]['thumbnailfile'] = NULL;
      }
      else {
        $items[$id][$delta]['thumbnail'] = intval($item['thumbnail']);
        $items[$id][$delta]['thumbnailfile'] = $files[$item['thumbnail']];
      }
    }
  }
}