I've got a module which defines a node type. I also need to attach a few fields to this node.

I'm defining the node in hook_node_info(), and I'm creating fields in hook_install(). However, isn't this going to create the fields before the node? Isn't hook_install() executed before hook_node_info(), meaning that I'll be attempting to attach the fields to a non-existing node type at that point?

How do I define a node type programatically, and also attach fields to the node type programatically upon module installation?


2 Answers 2


If you have familiarize with features module. It will be easy. You can get more idea about content type creation and it is related with field. Follow the steps to get a working example.

  1. Create content type and related field in you site.
  2. Enable features module and export this content type as a new module.
  3. The system generated module, you can find the content type creation and field relateion code. You can find documentation for creating content type and field via code in drupal.
  • I'd like not to use the features module. The module I'm creating should be "self contained". Also, if the features module is able to achieve this, then I assume I should be able to achieve it as well with my module.
    – sbrattla
    Mar 23, 2015 at 10:33
  • 1
    I am not asked you to use features module. Create a sample content type and fields in your test site. Then export that as a module. In that exported module, which contains the reference code for your field relation. Follow the steps to get a working example code. Mar 23, 2015 at 10:38
  • Alright, sorry, misinterpreted your answer.
    – sbrattla
    Mar 23, 2015 at 10:40

I eventually found a question which is more or less identical to this question. The accepted answer suggests that node_types_rebuild() gets invoked as the very first in hook_install(), and from there fields may be attached to defined node types.

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