Creating Field Groups in a Custom Module

If you need your module to add a fieldgroup to your Drupal 7 site, follow this quick and easy process: Also, if you’re looking to create fields, see this related post.

  1. Build the fieldgroup through the “manage fields” UI.
  2. Modify the code below to include a proper $fieldgroup_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. You should make sure this code is executed after its children are created (if you’re doing that in the same hook_install() implementation.
  5. Cleanup the formatting.
  6. Delete the fieldgroup you created through the UI. (warning: glance at step 8.2 below before continuing)
  7. Enable your module.
  8. Check if the fieldgroup shows up. If the fieldgroup doesn’t show up:
    1. Check if row exists in field_group table. If no, check logs for errors. Also, you can wrap dpm() around the field_group_group_save function to see the error as hook_install() runs.
    2. Make sure this fieldgroup has a “b” property value of “0” in the {variable} table’s “default_field_group” variable. It will not show in the form if it has a value of 1. I encountered this problem because deleting the fieldgroup (step 6 above) left that fieldgroup within default_field_group but set the value to 1. I would’ve expected the value to go away altogether, but it is not in my installation.

Example

Tagged with: , , , ,
Posted in Uncategorized
One comment on “Creating Field Groups in a Custom Module
  1. Thanks a lot for coping step 8. I’m happy to not be alone with it. Finally I solved it by variable_del('default_field_group'); after field_group_group_export_delete($group, FALSE); since deleting the default_field_group variable doesn’t have effect on any other existing field groups. Even more curious is that the default_field_group variable only gets set on a fresh Drupal instance after the first deletion of a field group.

Leave a Reply

Your email address will not be published. Required fields are marked *

*