Saving nodes with a filefield

旧城冷巷雨未停 提交于 2019-12-01 10:59:15

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.

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 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.

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.

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.

I have done something whith imagefield which worked, I think the structure has to be right otherwise it won't work. It took a lot of trial and error. This is is what I populated the imagefield with.

$image['data'] =array(
            'title' => $media_reference_attributes->getNamedItem("source")->value,
            'description' => $description,
            'alt' => "",);
$image['width'] = $width;
$image['height'] = $height;
$image['mimetype'] = $mime_type
$image['uid'] = 1;
$image['status'] = 1;
$image['fid'] = $fid;
$image['filesize'] = $file->filesize;
$image['nid'] = $id;
$image['filename'] = $url;
$image['timestamp'] = $file->timestamp;
$image['filepath'] = $file_path;

Hope this is of some help.

You might want to look at Image FUpload if you need a look at integrating the flash upload.

To push the files on to another server while still handling them through Drupal sounds a little like the CDN space, maybe look at the behavior in the CDN or CDN2 projects?

If you find a clear solution please come back and post it!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!