You are here

function scald_youtube_scald_add_form_fill in Scald YouTube 7

Implements hook_scald_add_form_fill().

File

./scald_youtube.module, line 127
Defines a YouTube provider for Scald.

Code

function scald_youtube_scald_add_form_fill(&$atom, $form, $form_state) {

  // Get the identifier.
  $identifier = scald_youtube_parse_id($form_state['values']['identifier'], FALSE);

  // Get video info.
  $infos = scald_youtube_video($identifier['id']);
  $atom->base_id = $identifier['id'];
  if (!isset($atom->data)) {
    $atom->data = array();
  }
  if (isset($identifier['list'])) {
    $atom->data['list'] = $identifier['list'];
  }
  $atom->title = $infos->title;

  // Prefill the author.
  if (isset($infos->author)) {
    $langcode = field_language('scald_atom', $atom, 'scald_authors');
    $atom->scald_authors[$langcode][0] = array(
      'tid' => 0,
      'taxonomy_term' => (object) array(
        'name' => $infos->author,
      ),
    );
  }

  // Prefill tags.
  if (isset($infos->tags)) {
    $langcode = field_language('scald_atom', $atom, 'scald_tags');
    foreach ($infos->tags as $index => $tag) {

      // Beware, this is not a real tid, it's just an index.
      $atom->scald_tags[$langcode][$index] = array(
        'tid' => $index,
        'taxonomy_term' => (object) array(
          'name' => $tag,
        ),
      );
    }
  }

  // Save video width and height.
  $atom->data['video_width'] = $infos->width;
  $atom->data['video_height'] = $infos->height;

  // Download a copy of the video thumbnail. This makes it possible
  // to do interesting manipulation with image styles presets.
  $thumb = drupal_http_request($infos->thumbnail['src']);
  if ($thumb->code == 200 && ($directory = ScaldAtomController::getThumbnailPath('video'))) {
    $dest = $directory . '/youtube-' . $infos->id . '.jpg';
    $file = file_save_data($thumb->data, $dest);
    if ($file) {

      // Set the file status to temporary.
      db_update('file_managed')
        ->condition('fid', $file->fid)
        ->fields(array(
        'status' => 0,
      ))
        ->execute();
      $langcode = field_language('scald_atom', $atom, 'scald_thumbnail');
      $atom->scald_thumbnail[$langcode][0] = (array) $file;
    }
  }
}