Creating Fields in a Custom Module

I’m going to be very brief here, because Joel Stein covers this beautifully. Also, if you’re looking to create fieldgroups, see this related post.
If you need your module to add a field to your Drupal 7 site, follow this quick and easy process:

  1. Build the field through the “manage fields” UI.
  2. Modify the code below to include a proper $field_name, $entity_type and $bundle_name. I needed to add a field to the user profile, so I set the $entity_type to “user” and the $bundle_name to “user”.
  3. Run this through the Devel “Execute PHP Code” page, or use Drush.
  4. Paste the output within your module’s hook_install() function within mymodule.install.
  5. Cleanup the formatting.
  6. Delete the field you created through the UI.
  7. Enable your module.
  8. Check if the field shows up.



Leave a Reply

Your email address will not be published.