1

I save a node with images which is beeing filled by a service. I write the image with drupal_write_record and the pictures appear in the node already. But when - at the end of the script - I call a node_save the image disappears again.

My Code:

  $file_drupal_path= $filedata['location'];
  $file = new stdClass();
  $file->filename = $filedata['name'];
  $file->filepath = $file_drupal_path; 
  $file->filemime = $filedata['mime'];
  $file->filesize = filesize($file_drupal_path);
  $file->filesource = $filedata['name'];

  $file->uid = 1;
  $file->status = FILE_STATUS_PERMANENT;
  $file->timestamp = time();  
  $file->list = 1;

  // fid is populated by drupal_write_record
  drupal_write_record('files', $file);

  $imageData = field_file_load($file->fid, TRUE);
  return $imageData;

and the node_save

function transport_service_save($node) {
    $node = (object) ($node);
    $node->promote = 1;
    node_save(node_submit($node));
    return print_r( $node , TRUE );
}

in the cck image field in the node there are keys with unset values as well.

Andreas
  • 11
  • 1

1 Answers1

1

Andreas,

Had the exact same problem.

Using drupal_execute() as described here fixed the problem immediately:

// Save the node, updated or new
// Get the node object as an array the node_form can use
$values = (array)$node;

// Save the node (this is like pushing the submit button on the node edit form)
drupal_execute('abc_node_form', $values, $node);

Source: http://www.drupalisms.com/gregory-go/blog/use-drupalexecute-instead-of-nodesave-to-save-a-node-programmatically

But a fair warning: it ran like a charm for the first few rounds, but now I get tonnes of errors type:

warning: call_user_func_array() [function.call-user-func-array]: 
First argument is expected to be a valid callback, 'node_form' was given in ...

Can't see what changed. All I did was call the page that did the saving a few times to test it.

And a final (hopefully!) edit to this reply. It seems that including the node.module file that contains node_form is needed, so adding this:

module_load_include('', 'node', 'node.pages.inc');

in your code (like in hook_init()) will do the trick.

Worked here, and now my nodes save with images intact.

SysDragon
  • 9,692
  • 15
  • 60
  • 89