Saving Nodes with a File Field

I am creating a bulk upload function for a Drupal site. Using flash, I can upload files to a specific URL, which then processes the files. What I want to do is not just upload files, but create a node of a certain type with a file saved in a file field that was configured using CCK. Since these are audio files, it is important that the file field processes the files, so add metadata can be provided with the getid3 module.

Now I looked through some of the code since I was unable to find the API documentation, but it is not clear how I should deal with this. Ideally, I could just pass the file to the function and just use the data returned when the node was saved, but I could not find this function.

If anyone has experience with this, I would appreciate some pointers on how to approach this issue.

+2
source share
3 answers

I had to do something similar a few weeks ago and ended up adapting some functions from the Remote File module , especially the function remote_file_cck_attach_file(). It uses a function field_file_save_file()from the file field module, which may be the function you are looking for.

In my case, files are extracted from several remote locations and temporarily saved using file_save_data(). Attaching them to the CCK file field occurs in the 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 - , node $index, 0 .

, verifyPath(). :

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?).');
  }
}

- node .

getid3, , . , / , , , , , field_file_save_file(). , , .

+8

-, , , , . . , .

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

, .

+1

You can look at FUpload Image if you need to look at Flash download integration.

To move files to another server, while still processing them through Drupal, is a bit like the CDN space, maybe look at the behavior in CDN or CDN2 projects?

If you find a clear solution, go back and post it!

+1
source

Source: https://habr.com/ru/post/1787676/


All Articles