Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi
I'm working on a site which distributes content (PDF's) to multiple subsites using url's in an XML.
If I would start from scratch I'd use the existing filefield mapper with filefield, but the current implementation uses upload.module.
This is my (draft) code, it already takes care of retrieving the PDF's, storing them and adding them in the files table.
It's not attaching the file to the nodesthough. I'm probably doing something wrong?
/**
* Implementation of hook_feeds_node_processor_targets_alter()
*/
function feeds_upload_mapper_feeds_node_processor_targets_alter(&$targets, $content_type) {
$info = content_types($content_type);
if (isset($info['extra']['attachments'])) {
$targets['attachment_field'] = array(
'name' => t('Attachment'),
'callback' => 'feeds_upload_mapper_feeds_set_target',
'description' => t('The URL for the attachment @name field of the node.', array('@name' => $info['extra']['attachments']['name'])),
);
}
}
/**
* Implementation of hook_feeds_set_target().
*
* @param $node
* The target node.
* @param $field_name
* The name of field on the target node to map to.
* @param $value
* The value to be mapped.
*
*/
function feeds_upload_mapper_feeds_set_target($node, $field_name, $value) {
$path = pathinfo($value);
$filedir = file_create_path('uploads');
file_check_directory($filedir, FILE_CREATE_DIRECTORY);
if(_feeds_upload_mapper_request($value)){
$file = file_get_contents($value);
$pathtofile = file_directory_path().'/uploads/'.$path['basename'];
if(!file_exists($pathtofile)) {
$file = file_save_data($file, $pathtofile, FILE_EXISTS_REPLACE);
if($file){
$file = new stdClass();
$file->filename = $path['basename'];
$file->filepath = $pathtofile;
$file->filemime = file_get_mimetype($pathtofile);
$file->filesize = filesize($pathtofile);
$file->timestamp = time();
$file->status = 1;
$file->uid = 1;
drupal_write_record('files', $file);
$fid = db_last_insert_id('files', 'fid');
$file->description = $path['basename'];
$file->list = 1;
$file->weight= 0;
$node->files[$fid] = $file; // <-------- WHAT EXACTLY SHOULD I DO HERE TO MAKE THIS WORK??
}
}
}
}
function _feeds_upload_mapper_request($url) {
$request = drupal_http_request($url, array(), 'GET', NULL, 0);
return ($request->code == '200' ? $request->data : FALSE);
}
any ideas?
Comment | File | Size | Author |
---|---|---|---|
#3 | feeds-core-upload-1160182-0.patch | 2.33 KB | Azol |
Comments
Comment #1
rv0 CreditAttribution: rv0 commentedwe decided switching to filefield anyway.
lowering priority and leaving this to "needs work" for anyone who might need this.
Comment #2
Azol CreditAttribution: Azol commentedI am in no way a Drupal pro, but this one works for me (based on rv0's code):
Filename upload.inc, should be put into feeds/mappers directory (without last ?>):
Suggestions?
Comment #3
Azol CreditAttribution: Azol commentedCreated patch for #2
Comment #6
twistor CreditAttribution: twistor as a volunteer commented