Hello how i can use the FileField Path to Programmatic create nodes in Drupal 7?

  $node = new stdClass();
  $node->type = 'article';
  node_object_prepare($node);
  $node->title    = 'Node Created Programmatically on ' . date('c');
  $node->language = LANGUAGE_NONE;

  $filepath = drupal_realpath('misc/druplicon.png');
  $file = (object) array(
    'uid' => 1,
    'uri' => $filepath,
    'filemime' => file_get_mimetype($filepath),
    'status' => 1,
  );
  $file = file_copy($file, 'public://');
  $node->field_image[$node->language][0] = (array)$file;

  node_save($node);

The above code works, but does not use the FileField Path.
How can I extend this to use it?

Comments

deciphered’s picture

Status: Active » Fixed

Tested the above code and it's working correctly with the latest dev of FileField Paths.

marcoka’s picture

i am infront of the same problem, your code is not good because you set your own path manually: $filepath = drupal_realpath('misc/druplicon.png');
'uri' => $filepath,

so normally it would loook like this with filefeld_paths processed:
http://screensnapr.com/v/CnFJHq.png

but as you set the URI by yourself there is no proecessing through filefield paths. i am currently searching how to get the path processed by filefield_paths.

deciphered’s picture

e-anima,

If you're referring to akalyptos' code when you say 'your code' I disagree, I tested that code with the latest dev of FileField Paths and confirmed that the file was processed correctly, as node_save() invokes the appropriate triggers for FileField Paths to modify the filepath.

If you have conflicting evidence can you provide a step-by-step guide on how to reproduce the issues you are having? Also, when you do so, make sure to set this issue as active, as it is still considered to be fixed.

Cheers,
Deciphered.

marcoka’s picture

Status: Fixed » Closed (fixed)

i did something wrong with the LANGUAGE_NONE constant, my bad. i can now confirm that it works as it should with that code above. apologys