Programmatic filefield value/Default Filefield value I have a module that takes a file uploaded to an entity, changes it and then saves the new version on my server in a folder based on the user id and entity title.

$where = 'private://' . $user-&gt;uid . '/';


This is all working fine.

I would like to have a field on the entity that displays this new file (it's an audio file).

I have tried writing the process of attaching the file into the module itself several times but have had no luck.

My next idea was to have Rules do this for me but unfortunately when it comes to setting the data value for a filefield Rules will not let you use PHP or Tokens so all I can do is set it to the value of an existing filefield.

My third idea was to have the entity's field have a default value and use tokens that reflect the location of the file e.g [uid]/[title] but filefield does not allow default values.

I have also tried writing a module that updates the entity field values but I can't seem to get this to work either.

Does anyone have any ideas on where you go from here? Does anyone know how this can be done?
The new version of the file should be saved as a managed file. This will provide a file object that has a fid which in turn can be used by the file field.

Here are the docs for the function used to save a managed file.

https://api.drupal.org/api/drupal/includes%21file.inc/function/file_save_data/7

The value for a file field contains the following

fid - The fid property of the file object returned by file_save_data.

description - The description may be used as the label of the link to the file. 

display - Determines whether a file should be displayed when outputting field content.