You are here

function video_embed_field_field_presave in Video Embed Field 7.2

Implements hook_field_presave().

Download and save the thumbnail if it hasn't already been stored. Get video data.

File

./video_embed_field.field.inc, line 228
Implement a video field.

Code

function video_embed_field_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  foreach ($items as $delta => $item) {

    // Trim whitespace from the video URL.
    $items[$delta]['video_url'] = trim($item['video_url']);

    // Try to load thumbnail URL.
    $info = video_embed_field_thumbnail_url($item['video_url']);
    if (isset($info['url']) && $info['url']) {
      $thumb_url = $info['url'];
      $thumb_extension = pathinfo($thumb_url, PATHINFO_EXTENSION);
      $local_path = "public://video_embed_field_thumbnails/{$info['handler']}/{$info['id']}.{$thumb_extension}";
      $dirname = drupal_dirname($local_path);
      file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
      $response = drupal_http_request($thumb_url);
      if (!isset($response->error)) {
        file_save_data($response->data, $local_path, FILE_EXISTS_REPLACE);
      }
      else {
        @copy($thumb_url, $local_path);
      }
      $items[$delta]['thumbnail_path'] = $local_path;

      // Delete any image derivatives at the original image path.
      image_path_flush($local_path);
    }
    else {
      $items[$delta]['thumbnail_path'] = '';
    }

    // Try to load video data.
    $data = video_embed_field_get_video_data($item['video_url']);
    if (is_array($data) && !empty($data)) {
      $items[$delta]['video_data'] = serialize($data);
    }
    else {
      $items[$delta]['video_data'] = NULL;
    }
  }
}