Hi! I'm trying to create nodes programatically and would like to populate a youtube field. After doing some research, I learned that the code needs to be something like this:

<?php
// $value in this case is the youtube ID.
$file = new stdClass();
  $file->uid = 1;
  $file->filename = $value;
  $file->uri = 'youtube://v/' . $value;
  $file->filemime =  'video/youtube';
  $file->type = 'video';
  $file->status = 1;
  $youtube = file_save($file);

node->field_youtube[$node->language]['0']['fid'] = (array) $youtube->fid;

However, it throws an error saying "Bad file type". I also tried writing the full url in $file->uri, then it showed up but wouldn't play. What am I doing wrong?

Comments

jaykainthola’s picture

Hi,
I also want to implement same.

Can anyone suggest us?

Thanks