I'm creating a module which uses the Form API and integrates with the Mantis bugtracker system. I have included a managed file field on the form and am at the stage of handling the form submission. The Form API documentation mentions that you must make a call to file_usage_add:

Without the call to file_usage_add, file_managed_file_validate produces an error upon saving the form, saying that the uploaded file may not be referenced.

Here is the signature for file_usage_add():

file_usage_add(stdClass $file, $module, $type, $id, $count = 1)

And the explanation of the parameters:

$file: A file object.

$module: The name of the module using the file.

$type: The type of the object that contains the referenced file.

$id: The unique, numeric ID of the object containing the referenced file.

$count: (optional) The number of references to add to the object. Defaults to 1.

The problem I have is that my module doesn't create any nodes to attach the file to. I just want to store the file in the filesystem and then send it off to Mantis.

Any suggestions on what I can enter for the $type and $id parameters?

  • What about attaching it to user 1? Are there any problems with this approach bearing in mind that there could be potentially quite a lot of files attached over time? Mar 5, 2013 at 10:31
  • Why a downvote?l May 2, 2014 at 8:28

1 Answer 1


I attached the file to user 1 and can't see any adverse effects as yet:

file_usage_add($file, 'mantis', 'user', 1);

I also OMITTED the following line from my submit function:

$file->status = FILE_STATUS_PERMANENT;

This means that the file will be deleted after 6 hours when cron runs.

If anyone has a better solution then I'll be happy to mark as answer.

  • I am also not sure how to handle the id on submit or import when the $node id is not available
    – latvian
    May 1, 2014 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.