I have found the answer. The file_save function only checks the file identifier in the database. However, the youtube uri field did not allow duplicates. Therefore, I stole this function from the file_example module. It checks if a file exists with this uri if it loads an object.
function file_example_get_managed_file($uri) { $fid = db_query('SELECT fid FROM {file_managed} WHERE uri = :uri', array(':uri' => $uri))->fetchField(); if (!empty($fid)) { $file_object = file_load($fid); return $file_object; } return FALSE; }
So, at the end, I simply put the if statement, for example:
$file_exists = wthm_get_managed_file('youtube://v/' . $value); if (!$file_exists) { $file_path = drupal_realpath('youtube://v/' . $value); $file = new stdClass(); $file->uid = 1; $file->filename = $value; $file->uri = 'youtube://v/' . $value; $file->filemime = file_get_mimetype($file_path); $file->type = 'video'; $file->status = 1; $file_exists = file_save($file); } $node->field_youtube[$node->language]['0'] = (array) $file_exists;
This solved most of the problems. I still get a message about a bad file extension, but it still works.
source share