【问题标题】:Drupal 7 - create node programatically, adding a youtube embed to a fieldDrupal 7 - 以编程方式创建节点,将 youtube 添加到字段中
【发布时间】:2011-07-31 23:53:47
【问题描述】:

我正在尝试以编程方式创建节点。使用带有 youtube 扩展的媒体模块,我想用 youtube 数据填充一个字段。从我目前所读到的内容来看,它看起来像这样:

   <?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;
    ?>

我通过查看 bartik 主题中 $content 变量中的信息了解到这一点。但是,这会导致“错误的文件扩展名”错误。我还尝试将整个 url 放入 $file->uri 并在其上使用 file_get_mimetype,然后它没有抛出错误,但视频也不起作用。有谁知道怎么做?

【问题讨论】:

    标签: php drupal-7


    【解决方案1】:

    我找到了答案。函数 file_save 仅检查文件 ID 是否已在数据库中。但是,youtube uri 字段不允许重复。因此我从 file_example 模块中窃取了这个函数。它检查是否存在具有该 uri 的文件,如果存在则加载对象。

    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;
    }
    

    所以最后我只是简单地放了一个 if 语句,像这样:

    $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;  
    

    这解决了大多数问题。我仍然收到一条消息说文件扩展名错误,但它仍然有效。

    【讨论】:

      【解决方案2】:

      我让它像这样工作。我正在导入需要解析的嵌入代码,有些是骗子,我认为这个函数file_uri_to_object($code, $use_existing = TRUE) 允许您重用托管网址。 $r->video 是 youtube 的 iframe 嵌入代码,它被解析为正确的 uri 格式

        // 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】:

        更好的方法:

            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;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多