Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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? – littledynamo Mar 5 '13 at 10:31
Why a downvote?l – littledynamo May 2 '14 at 8:28
up vote 2 down vote accepted

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.

share|improve this 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 '14 at 21:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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