3

In my module, I define a custom content type, and custom fields by using hook_node_info() hook_field_info(), and so on. I'd like to programmatically add field instances of my field to the created content type when my module is enabled/installed.

I know there is the possibility of creating the content type, and the field in the database by using node_type_save() and field_create_instance(), but this is not what I want, since during the development the field definitions will change, and in the latter scenario I would have to uninstall and reinstall the module every time I change a field/content type.

I tried using field_create_instance() in my module's hook_install() and hook_enable(), but Drupal says that the field I try to add does not exist or is inactive.

Is there an elegant way to let the module define fields, and attach them to content types when enabled (and of course detach them on uninstall or disabling it)?

2 Answers 2

1

Field helper attempts to handle the creation/deletion of fields and instances for you and allows you to define them in hooks. It will not update the field definitions since that is not something Drupal core supports. Otherwise Features handles creation of field instances on content types if you were to make your module a feature.

0

there is also a sandbox with Field Fairy. Due to it being a sandbox, I wouldn't recommend it for production.

Your Answer

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

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