Hi,

I'm trying to save the path of an image, but after node_save($node); the dump inside my newly created node, is giving me only an empty $node->field_image_cache[0] object. The other values are saved. I don't know, which reference I should define. Thats my current status:

$node = new StdClass();
$node->uid = 4;
$node->type = 'product';
$node->status = 1;
$node->title = 'test';
$node->body = 'body';
$node->field_notice[0]['value'] = 'Testtext';
$node->field_image_cache[0]['filepath'] = 'sites/default/files/woman.png'; // is not saving
$node->field_image_cache[0]['value'] = 'sites/default/files/woman.png'; // is not saving

//save the node
node_save($node);

Hope somebody can help me. Best regards from Germany!

Comments

nevets’s picture

Your file needs to be first saved to the files table and the new fid used when saving the node.

jepster_’s picture

Thanks for your reply. The example path with

$node->field_image_cache[0]['filepath'] = 'sites/default/files/woman.png';

is based on a file which is already uploaded and for which a fid is already created in the database. So how I can save my node with imagecache data?

nevets’s picture

Not sure of your goal, you save images with nodes, not image cache data. When you save a file/image with a node you save the fid.

jepster_’s picture

When I save an image to the files-table, so how I can associate it with a specific node? I cannot find anything about that with googleing about drupal's node api.

nevets’s picture

If you use drupal_write_record() to save the file, the data will have fid filled in on return which you can use with the file/image field when saving the node. Something like

$file = new stdClass();
// Fill in $file with fields for table 'files'
drupal_write_record('files', $file);
// At this point you can use $file->fid
jepster_’s picture

After having a FID I still cannot save the image as a image cache content type. A simple image, without a image cache preset is working not, as well.

Here I'm:

Image cache array

$form_state['values']['field_image_cache']['0'] = array(
		array(
		  'fid' => $file->fid,
		  'nid' => $highestNodeID,
		  'title' => basename($file->filename),
		  'filename' => $file->filename,
		  'filepath' => $file->filepath,
		  'filesize' => $file->filesize,
		  'status' => '1',
		  'timestamp' => time(),
		  'list' => '1',
		  'filemime' => $mime,
		  'uid' => '1',
		  '_weight' => '0',
		  'alt' => '',
		  'title' => '',
		  'filefield_upload' => 'Upload',
		  'filefield_remove' => 'Löschen',
		  'upload' => '',
		  'data' => array(
			'alt' => 'test',
			'title' => 'test'
		  )
		),
	  );

Array for simple image data field

$form_state['values']['field_shots_image'] = array(
    array(
      'fid' => $file->fid,
      'title' => basename($file->filename),
      'filename' => $file->filename,
      'filepath' => $file->filepath,
      'filesize' => $file->filesize,
      'mimetype' => $mime,
      'description' => basename($file->filename),
      'list' => 1,
    ),
  );

I'm saving with

drupal_execute($your_node_type.'_node_form', $form_state, (object)$node);

Hope anybody can help me.

geraldito’s picture

The only way I could make get it work was using Form API and a custom CCK field as described here: http://thedrupalblog.com/upload-image-using-forms-api-and-attach-it-imag...

ram4nd’s picture

I had the same problem, I wrote a little snippet with a solution that I used.

Save image programmatically in Drupal 7

fizk’s picture

Status: Active » Closed (fixed)

Please reopen if this is still an issue with ImageCache 6.x-2.0-rc1.