CCK Filefield and drupal_execute

scb - July 4, 2008 - 07:53

Does anyone know how to create a node with a cck filefield using drupal_execute? I have seen some similar examples, but never with filefield itself, and cannot get it to work...
I've tried this, but something's not working: ($v holds the data for the node)

<?php
                $node
= array('type' => $node_type, 'name'=>'admin');
               
$values["field_foo_upload"] = array();
               
$values["field_foo"] = array(
                    array(
                         
'description' => $v['original_filename'],
                         
'delete' => 0,
                         
'list' => 1,
                         
'filename' => $v['filename'],
                         
'filepath' => $v['filepath'],
                         
'filemime' => $v['filemime'],
                         
'filesize' => $v['filesize'],
                         
'fid' => 'upload',
                         
'previous_filepath' => $v['filepath'],
                   ));
               
$res = drupal_execute("{$node_type}_node_form", $values, (object) $node);
?>

I've tried mith many combinations similar to this, but never got it to work. Although I've also tried the same approach for imagefield, and it works perfectly... this way:

<?php
$values
["field_bar"] = array(
                array(
                 
'fid' => 'upload',
                 
'title' => $v['title'],
                 
'filename' => $v['filename'],
                 
'filepath' => $v['filepath'],
                 
'filesize' => $v['filesize'],
                ),
              );
?>

but no luck for the filefield... has anyone done this? I've also tried with the standard upload.module attachment, also no luck...

Thanks!

P.S. I've also posted this in http://drupal.org/node/131704 but the conversation about drupal_execute is spread through too many threads... there should be a doc page with all the examples together!

More drupal_execute and cck related info:
http://drupal.org/node/178506
http://www.civicactions.com/blog/cck_import_and_update
http://www.lullabot.com/articles/quick_and_dirty_cck_imports

drupal_execute and filefield

jwbuzz - July 9, 2008 - 20:17

I just got this working with this code...

// Move $_FILES around to the way filefield is expecting.
$_FILES['files']['name']['field_filefield_upload'] = $_FILES['Filedata']['name'];
$_FILES['files']['type']['field_filefield_upload'] = $_FILES['Filedata']['type'];
$_FILES['files']['tmp_name']['field_filefield_upload'] = $_FILES['Filedata']['tmp_name'];
$_FILES['files']['error']['field_filefield_upload'] = $_FILES['Filedata']['error'];
$_FILES['files']['size']['field_filefield_upload'] = $_FILES['Filedata']['size'];
// Using the helpful tip from http://drupal.org/node/131704#comment-890095
$node = array('type' => 'content_document');

//Load up the node object for saving
$values = array(
'title' => $_FILES['files']['name']['field_filefield_upload'],
'uid' => 1,//Shouldn't hard code the userid
'name' => 'admin',//Shouldn't hard code the user
'field_filefield_upload' => array(),
);

drupal_execute('content_document_node_form', $values, $node);

filefied and node_save

pinkqui - September 10, 2008 - 16:59

I had problem with dupal_execute. I used node_save. It worked pretty well.

        $node->field_myfileuploadfield[id][description] ='description';
        $node->field_myfileuploadfield[id][delete] = 0;
        $node->field_myfileuploadfield[id]['list'] = 1;
        $node->field_myfileuploadfield[id][filename] = $name;
        $node->field_myfileuploadfield[id][filepath] = $path;
        $node->field_myfileuploadfield[id][filemime] = mimedetect_mime($path);
        $node->field_myfileuploadfield[id][fid] = 'upload';
        $node->field_myfileuploadfield[id][previous_filepath] = $path;
        node_save( $node);

what worked for me

grendzy - November 11, 2008 - 23:04

For some reason, when I try this the node and file objects both get saved, but the filefield isn't populated. As a workaround, I was able to manually insert the records in the database table:

<?php
  node_save
(&$node);

 
$result = db_query("INSERT INTO {content_type_podcast} (vid, nid, field_audio_fid, field_audio_list, field_audio_data) " .
 
" VALUES (%d, %d, %d, %d, \"%s\") ", $node->vid, $node->nid, $file->fid, 1, 'a:0:{}');
?>

avoid it

drewish - January 2, 2009 - 20:21

i'd avoid drupal_execute and try this techniques: http://drewish.com/node/120 and http://drupal.org/node/292904

 
 

Drupal is a registered trademark of Dries Buytaert.