-
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>');