Hey, I am creating custom content type programmatically and was wondering if there was any way to programmatically assign the field instances to a field group I have already created?

Thanks

Comments

Ehud’s picture

Subscribing

ndeschildre’s picture

Hello,

I just happened to search for it, and came up with this working bit of code:

$groups = field_group_read_groups(array(
    'name' => 'node',
    'bundle' => 'article',
    'view_mode' => 'full'));
  $your_group = $groups['node']['article']['form']['group_your_group'];
  $your_group->children[] = 'field_your_new_field';
  field_group_group_save($your_group);
acbramley’s picture

Status: Active » Closed (fixed)

Thanks @ndeschildre that worked great, think this should be closed now.

tariqinam’s picture

Issue summary: View changes

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

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

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

srinivasraod’s picture

Hi,
You can try with with this if you get an empty array....

$groups = field_group_read_groups();
$your_group = $groups['node']['article']['form']['group_your_group'];
$your_group->children[] = 'field_your_new_field';
field_group_group_save($your_group);