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?

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 Jun 24 '17 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';
field_group_group_save($your_group);
  • 3
    I want to alter the form! – S. A. Malik May 2 '13 at 12:54
  • 2
    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/… – Maciej Zgadzaj May 2 '13 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 May 25 '16 at 17:08
  • This provides a way to re-define field definitions, not to do a form_alter - not what the OP is asking. – aaronbauman Apr 2 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'];
    unset($form['example_field']);
    // Further adjustments as necessary
  }
}

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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