4

I have a custom calendar module I've created. When a user creates an event they have the ability to attach a thumbnail image to it. I'm trying to make it use the managed_file type. In the documentation it appears I need to use file_usage_add to keep it from throwing an error. The problem is I'm not sure what to put in the 'type' and 'id' parameters. The documentation lists these required 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.

$file and $module are easy. However, this module doesn't create a node or anything else. I have my own table structure for this information. Is there a way to define my table that events get stored in as a 'type' and pass that with the unique id from that table?

1 Answer 1

4

Yes, type just needs to be something unique within your module's types of file usage. The value of the string itself is arbitrary.

So if you have a module which stores pictures of cats and dogs, and you want to address those two different types of usage separately, you might use (contrived examples obviously):

$cat = MYMODULE_load_cat('whiskers');
file_usage_add($file, 'MYMODULE', 'cat_pictures', $cat->id);

$dog = MYMODULE_load_dog('fido');
file_usage_add($file, 'MYMODULE', 'dog_pictures', $dog->id);

But the type for those could just as easily be cat_foo and dog_foo, all that matters is that you (the module developer) know what they are, so they can be used appropriately with file_usage_delete().

6
  • Thanks. Where do you define the type though? Since it has to have an id I'm assuming I have to declare this object class type somewhere.
    – LoneWolfPR
    Jun 2, 2014 at 17:03
  • Oh, never mind. I see what you're saying. In my case I would just use a constant type, say 'event_thumb'. then in the id I would give it the id for my event that the thumbnail is attached to, say $event->id. Is that correct?
    – LoneWolfPR
    Jun 2, 2014 at 17:07
  • Yep, exactly right
    – Clive
    Jun 2, 2014 at 17:11
  • 4
    Sorry, I have the same problem (more or less) and I don't understand the answer in the comment: what should I use for 'id'? Something random? The session id? There is no node yet, so I can't use a node-id?
    – Ideogram
    Jun 10, 2015 at 12:55
  • 1
    I am creating a custom form with a file upload option. Its just a stand-alone form. What do I mention for 'id'? May 15, 2018 at 8:31

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.