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

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

问题描述

我正在为Drupal网站创建批量上传功能。使用Flash我可以将文件上传到特定的url,然后处理文件。我想做的不仅仅是上传文件,而是创建一个特定类型的节点,文件保存到已经使用CCK设置的文件夹中。因为这些是音频文件,文件区域处理这些文件很重要,所以可以在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() presave,

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天全站免登陆