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.
- Build the fieldgroup through the “manage fields” UI.
- 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”.
1234567891011$fieldgroup_name = ''; // e.g., group_author_info$entity_type = ''; // e.g., node$bundle_name = ''; // e.g., storyinclude_once DRUPAL_ROOT . '/includes/utility.inc';$group = field_group_load_field_group($fieldgroup_name, $entity_type, $bundle_name, 'form');$group_var = '$' . $fieldgroup_name . ' = ' . substr(drupal_var_export($group), 9) . ";\r";$group_var = preg_replace("/ 'id'.*,\n/", '', $group_var); // remove the ID property$group_var = preg_replace("/ 'export_type'.*,\n/", '', $group_var); // remove the export_type property$output = $group_var . 'field_group_group_save($' . $fieldgroup_name . ');';drupal_set_message("<textarea rows=30 style=\"width: 100%;\">" . $output . '</textarea>'); - Run this through the Devel “Execute PHP Code” page, or use Drush.
- 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.
- Cleanup the formatting.
- Delete the fieldgroup you created through the UI. (warning: glance at step 8.2 below before continuing)
- Enable your module.
- Check if the fieldgroup shows up. If the fieldgroup doesn’t show up:
- 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.
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/** * Implements hook_install(). */ function activecollab_install() { $group_activecollab_settings = (object) array( 'identifier' => 'group_activecollab_settings|user|user|form', 'group_name' => 'group_activecollab_settings', 'entity_type' => 'user', 'bundle' => 'user', 'mode' => 'form', 'parent_name' => '', 'table' => 'field_group', 'type' => 'Overridden', 'disabled' => FALSE, 'export_module' => 'activecollab', 'label' => 'Active Collab Settings', 'weight' => '3', 'children' => array( 'field_activecollab_apikey', ), 'format_type' => 'fieldset', 'format_settings' => array( 'formatter' => 'collapsible', 'instance_settings' => array( 'description' => '', 'classes' => '', 'required_fields' => 1, ), ), ); field_group_group_save($group_activecollab_settings); } |
2 Comments
Norman Leymann
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');
afterfield_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.Denis
Thanks!
variable_del(‘default_field_group’);
Worked for me