I have used "Field Group" module and created some groups in backend. Now i want to programatically add a field to the group. How can I do that.

I am using Hook_form_alter, I have noticed that every field has an 'und'attribute in which the field group it belongs to is listed, but if a create and 'und' array and add a field group name to it, it does not work.

How should I do it?


5 Answers 5


This is a bit old, but I wanted to do the same thing and couldn't find an answer on the web. I ended up figuring it out by myself.

Let's suppose you created a field group through the CMS interface with the name "group_product_fields". You added some fields to that group.

Now you have a new field being included in the form programatically via hook_form_alter. You want to add that field to "group_product_fields". Here is an example of what the field could be:

$form['new_product_field'] = array(
  '#type' => 'textfield',
  '#title' => t('New product field'),
  '#description' => t('Description for this new product field'),

All you need to do now is to add that new field to the field group. To do that add the line below to the hook_form_alter. It can be placed anywhere inside it.

$form['#group_children']['new_product_field'] = 'group_product_fields';

That's all. You may wish to adjust the field weight for it to be positioned as you want. But in the end this is really simple. :)

  • This should be the accepted answer, IMO.
    – m.stenta
    Commented Jun 24, 2017 at 13:29

Quoting after Programtically add fields to a field group:

$groups = field_group_read_groups(array(
  'entity_type' => 'node',
  'bundle'      => 'article',
  'mode'   => 'full'
$your_group = $groups['node']['article']['form']['group_your_group'];
$your_group->children[] = 'field_your_new_field';
  • 3
    I want to alter the form! Commented May 2, 2013 at 12:54
  • 3
    Then I don't see why you want to use Field Group module? Just define a fieldset(s) and move your fields there. For example: drupal.stackexchange.com/questions/4049/… Commented May 2, 2013 at 13:02
  • We can use 'mode' => 'form' or 'mode' => 'default' instead of 'mode' => 'full', or just omit mode at all. Any view_mode_name, thanks @Maiq Fash
    – graceman9
    Commented May 25, 2016 at 17:08
  • This provides a way to re-define field definitions, not to do a form_alter - not what the OP is asking. Commented Apr 2, 2018 at 18:37

Not sure how it worked for you. Because array key name must match with database field name. And They are:

| entity_type | varchar(32)
| bundle | varchar(128)

| mode | varchar(128)

I have to use following format to get it working. let me know if i am doing something silly here.

$groups = field_group_read_groups(array(
  'entity_type' => 'node',
  'bundle' => 'bundle type',
  'mode' => 'view_mode_name'));

Take a look at hook_field_group_build_pre_render_alter().

This provides you with an opportunity to change the structure of your groups programmatically.

For example, move the field "example_field" into the group "group_example" from the root form element:

function EXAMPLE_forms_field_group_build_pre_render_alter(&$form) {
  if (example_condition()) {
    $form['group_example'] = $form['group_example']['example_field'];
    // Further adjustments as necessary

First, find your field group machine name in the CMS panel then in your form alter do

$form['#group_children']['FIELD_MACHINE_NAME'] = 'GROUP_MACHINE_NAME';

for an instanst, I've added a custom field mail into the User form field group field group :

$form['#group_children']['mail'] = 'group_user_form_field_group';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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