使用文件字段保存节点 [英] Saving nodes with a filefield

查看:13
本文介绍了使用文件字段保存节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Drupal 站点创建批量上传功能.使用 flash,我可以将文件上传到一个特定的 url,然后处理这些文件.我想要做的不仅仅是上传文件,而是创建一个特定类型的节点,并将文件保存到已使用 CCK 设置的文件字段中.由于这些是音频文件,filefield 处理这些文件很重要,因此可以通过 getid3 模块提供额外的元数据.

I'm in the progress of creating a bulk upload function for a Drupal site. Using flash I'm able to upload the files to a specific url that then handles the files. What I want to do, is not just to upload the files, but create a node of a specific type with the file saved to a filefield that has been setup with CCK. Since these are audio files, it's important that filefield handles the files, so addition meta data can be provided with the getid3 module.

现在我已经浏览了一些代码,因为我找不到 API 文档,但我根本不清楚我应该如何处理这个问题.理想情况下,我可以将文件传递给一个函数,并仅使用保存节点时返回的数据,但我一直无法找到该函数.

Now I've looked through some of the code as I wasn't able to find an API documentation, but it's not clear at all how I should handle this. Ideally I could just pass the file to a function and just use the data returned when saving the node, but I haven't been able to find that function.

如果有人有这方面的经验,我会很感激一些关于如何处理这个问题的指示.

If any one has experience with this I would apreciate some pointers on how to approach this matter.

推荐答案

几周前我不得不做类似的事情,最终调整了 远程文件模块,尤其是remote_file_cck_attach_file()函数.它使用 filefield 模块中的 field_file_save_file() 函数,这可能是您正在寻找的函数.

I had to do something similar some weeks ago and ended up adapting some functionality from the Remote File module, especially the remote_file_cck_attach_file() function. It uses the field_file_save_file() function from the filefield module, which might be the function you're looking for.

就我而言,文件是从多个远程位置获取并使用 file_save_data().将它们附加到 CCK 文件字段发生在 hook_nodeapi() 预保存时,使用以下内容:

In my case, the files are fetched from several remote locations and stored temporarily using file_save_data(). Attaching them to a CCK filefield happens on hook_nodeapi() presave, using the following:

public static function attachAsCCKField(&$node, $filepath, $fieldname, $index=0) {
  // Grab the filefield definition
  $field = content_fields($fieldname, $node->type);
  $validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
  $fieldFileDirectory = filefield_widget_file_path($field);
  // This path does not necessarily exist already, so make sure it is available
  self::verifyPath($fieldFileDirectory);
  $file = field_file_save_file($filepath, $validators, $fieldFileDirectory);
  // Is the CCK field array already available in the node object?
  if (!is_array($node->$fieldname)) {
    // No, add a stub
    $node->$fieldname=array();
  }
  $node->{$fieldname}[$index] = $file;
}

$filepath 是应该附加的文件的路径,$fieldname 是要在节点内使用的文件字段实例的内部名称,$在多个字段条目的情况下,index 将是附件的基于 0 的索引.

$filepath is the path to the file that should be attached, $fieldname is the internal name of the filefield instance to use within the node and $index would be the 0 based index of the attached file in case of multiple field entries.

该函数最终出现在一个实用程序类中,因此是 verifyPath() 调用的类语法.该调用只是确保目标目录可用:

The function ended up within a utility class, hence the class syntax for the verifyPath() call. The call just ensures that the target directory is available:

public static function verifyPath($path) {
  if (!file_check_directory($path, FILE_CREATE_DIRECTORY)) {
    throw new RuntimeException('The path "' . $path . '" is not valid (not creatable, not writeable?).');
  }
}

这对我来说是成功的 - 其他一切都发生在节点自动保存上.

That did it for me - everything else happens on node saving automatically.

我还没有使用过 getid3 模块,所以我不知道它是否可以与这种方式一起使用.此外,我不需要向文件字段添加额外的信息/属性,所以也许您必须将更多信息放入字段数组,而不仅仅是 field_file_save_file() 返回的文件.无论如何,希望这会有所帮助并祝您好运.

I have not used the getid3 module yet, so I have no idea if it would play along with this way of doing it. Also, I had no need to add additional information/attributes to the filefield, so maybe you'd have to put some more information into the field array than just the file returned by field_file_save_file(). Anyways, hope this helps and good luck.

这篇关于使用文件字段保存节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆