Drupal 7 - create a node programmatically by adding youtube tab to the text box

I am trying to create nodes programmatically. Using a Media module with the youtube extension, I would like to populate the field with youtube data. From what I have read so far, it will look 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; ?> 

I found out about this by looking at the information in the $ content variable in the bartik topic. However, this results in a Bad File Extension error. I also tried putting the whole url in $ file-> uri and using file_get_mimetype on it, then it did not throw an error, but the video did not work either. Does anyone know how to do this?

+4
source share
3 answers

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.

+5
source

I worked like that. I am importing embed codes that need to be parsed, and some are cheats, and I think this function file_uri_to_object($code, $use_existing = TRUE) allows you to reuse managed URLs. $ r-> video is the iframe embed code for youtube, which is processed in the correct uri format.

  // include the media youtube handler.inc file to use the embed code parsing $path = drupal_get_path('module','media_youtube').'/includes/MediaInternetYouTubeHandler.inc'; require_once($path); $code = MediaInternetYouTubeHandler::parse($r->video); $youtube = file_uri_to_object($code, $use_existing = TRUE); $youtube->display = 1; $youtube = file_save($youtube); $node->field_video[$lang][0] = (array)$youtube; node_save($node); 
+3
source

The better:

  module_load_include('inc', 'media_youtube', 'includes/MediaInternetYouTubeHandler.inc'); $obj = new MediaInternetYouTubeHandler($url); $file = $obj->getFileObject(); $file->display = 1; file_save($file); $product->field_product_video[LANGUAGE_NONE][] = (array) $file; 
+3
source

All Articles