Hey, its me again! ;-)

I have made som modulecode to create a node with some files attached. It works "fine".
My problem is how do I get the correct path where the file should be saved? The path is defined in the ContentType definition on the filefield.

    $filepath = 'public://'.$node->field_portfolio_item_image[0]['filepath'];
//debug($filepath);
    file_prepare_directory($filepath);
    $file = file_save_data(base64_decode($bArray), $filepath . $filename, FILE_EXISTS_RENAME);
    $node->field_portfolio_item_image = array(LANGUAGE_NONE => array('0' => (array)$file));
  }
...
node_save($node)

Regards Lars

Comments

larsmw’s picture

When I add a node of this type through the UI then the right upload-path for the image field is used. So which function call do I miss to get the file in the right directory?

/Lars Nielsen

larsmw’s picture

I have tried to hook into node_presave and node_insert to get information about the filepath. But both places I only get basic information about the managed file being created.

When in the node creation process can I get information about where the file should be stored?

/Lars Nielsen

jaypan’s picture

You store the file wherever you want. If you use private:// and public://, it will save the files to the filepath that these protocols are set to.

Contact me to contract me for D7 -> D10/11 migrations.

larsmw’s picture

Hi Jay,
I am not sure if I have expressed my self clear enough. I am saving the node through a webservice-method, so I dont have an instance of a form. And I am using the public:// scheme to save the file and it ends up in the public files/ folder. I need to get the information about the target-path from the field-settings on the content-type. The path is defined with some tokens in the content type which need to get resolved to the actual real path.

/Lars Nielsen

jaypan’s picture

I'm a little confused, if you are setting the scheme to public://, and the file is being saved in the public files folder, then what is not working?

Contact me to contract me for D7 -> D10/11 migrations.

larsmw’s picture

It is supposed to to be moved to a subfolder under files, which is definde in the imagefield.

I have made the following code which almost works :

$params = array('entity_type' => 'node', 'bundle' => 'portfolio_item');
$fieldinstance = field_read_instances($params);
$field = field_info_field('field_image');
$filefield_data = file_field_widget_uri($field, $fieldinstance);

My issue now is that file_field_widget_uri complains that 'settings' is not set in $field. But when I debug field I can see a settings subarray with uri_scheme amongst other things.

/Lars Nielsen

larsmw’s picture

This works now! :-) My problem was that there were multiple instances so I had to use an index on $fieldinstance.

$params = array('entity_type' => 'node', 'bundle' => 'my_type');
$fieldinstance = field_read_instances($params);
$field = field_info_field('field_my_image');
$filefield_data = file_field_widget_uri($field, $fieldinstance[1]);

/Lars Nielsen