Hello, all.
I'm writing a custom module, which is NOT dependent on CCK. However, it will involve image handling, and I'd like to have imagefield support available as an option.
The module is designed to pull information from Discogs using REST. So, images will be pulled from the Discogs database, saved to Drupal's files directory, and then attached to my custom node type.
Right now, it can either use "native" image handling (which just stores the image URL's), or use Image's image_attach module. I'd like to also support imagefield, but only if that module is installed (so it won't actually be a dependency).
This means that I would need to do a check to see if imagefield is available (presumably using module_exists()), and if so, create a custom imagefield for that node. I'd like to do all this during the installation process, but I understand there are some issues with that.
I've read through the documentation for CCK, filefield, and imagefield, and I think I have a handle on inserting information into that custom field once it's created. However, I did not find any information about a way to create the custom field itself, without any user interaction whatsoever.
Is there a way to do this? If anyone can even point me in the right direction, I'll be eternally grateful. I'd even add a page to the documentation if it would help.
For the record, I'm using the latest stable versions:
Drupal 6.16
CCK 6.x-2.6
FileField 6.x-3.2
imagefield 6.x-3.2
Comments
Comment #1
quicksketchPretty much everything you need is in content.crud.inc, part of CCK. See the function listing: http://api.lullabot.com/file/contrib/cck/includes/content.crud.inc. You'll just need to use content_field_instance_create() I believe.
Comment #2
quicksketchHere's a good example from Ubercart, doing the same thing: http://api.lullabot.com/uc_product_add_default_image_field
Comment #3
Karlheinz commentedThanks very much for this. Both those links are immensely helpful, especially the second. I'm surprised the whole of that site hasn't been incorporated into the handbook pages here on Drupal.org. I'm going to mess around with that code in the next few days, and if it's OK with everyone, I'll post a handbook page about what I learn.
Comment #4
Karlheinz commentedHere's the code I came up with:
This code will either create or delete the imagefield, depending on what value you pass to the
$createvariable. I also included code to set animagefield_activevariable, so you can tell whether or not the imagefield is there if you programatically generate content for it.Also,
$fieldis the entire CCK export from my custom field, but I'm pretty sure you could just leave off the array fields that are empty.This function is called by
hook_install(), so the field will automatically be generated when the module is installed. (hook_uninstall()calls the function with$create = FALSE.)If you have any criticisms, please let me know. It's tested and functional using the versions in my first post.
Comment #5
quicksketchFeel free to post a handbook page under http://drupal.org/handbook/modules/filefield, any user with an account Drupal.org has permissions to do so.
Comment #7
broncomania commentedThx for sharing your code Karlheinz. this is exactly what I need. Hope their are no changes during the cck development :-)