Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal 7 - create node programatically, adding a youtube embed to a field

Tags:

php

drupal-7

I'm trying to create nodes programatically. Using Media module with the youtube extension, I'd like to populate a field with youtube data. From what I've read so far, it's going to 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 learned this by looking at the information in the $content variable in the bartik theme. 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 didn't throw an error but the video didn't work either. Does anyone know how to do this?

like image 969
Toxid Avatar asked Dec 21 '22 10:12

Toxid


2 Answers

I found the answer. The function file_save only checks if a file id is already 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 that uri, if it does it loads the 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 in the end I simply put an if statement, like this:

$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 problems. I still get a message saying bad file extension, but it works anyway.

like image 83
Toxid Avatar answered Dec 24 '22 02:12

Toxid


I got it working like this. I'm importing embed codes that need to be parsed and some are dupes, and i think this function file_uri_to_object($code, $use_existing = TRUE) allows you to reuse managed urls. $r->video is an iframe embed code for youtube which gets parsed to 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); 
like image 45
steev Avatar answered Dec 24 '22 02:12

steev