Steps to reproduce:
1. Create a node form field group programmatically with field_group_group_save($group)
2. Delete this field group manually, or delete it programmatically with field_group_group_export_delete($group, FALSE), doesn't matter
3. Create the field group from step 1 again programmatically with field_group_group_save($group)

When trying to recreate the field group again it simply won't appear in its place (node form), although all database entries are correct.

I observed that only on deletion of the field group an entry gets set in the variable table: default_field_group. That then seems to prevent the field group from appearing correctly again. When I delete that variable before programmatically recreating the field group everything works fine. When I recreate it manually everything works fine anyways. But I need to do that in code. Since deleting the default_field_group variable doesn't seem to have any effect on existing field groups, I think I'll take that shortcut for now and simply delete it at the same time I'm deleting a field group.

Is this expected behaviour? Is it mandatory to delete or modify the default_field_group variable on programmatically deletion of a field group?

Here ist the code I used for creation:

$group_name = 'MYGROUPNAME';
$entity_type = 'node';
$bundle = 'MYNODETYPE';
$mode = 'form';
if (!field_group_exists($group_name, $entity_type, $bundle, $mode)) {
  $group = (object) array(
    'identifier' => $group_name .'|'. $entity_type .'|'. $bundle .'|'. $mode,
    'group_name' => $group_name,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'mode' => $mode,
    'label' => 'MYLABEL',
    'weight' => '1',
    'children' => array('MYFIELD1', 'MYFIELD2'),
    'format_type' => 'div',
    'format_settings' => array(
      'label' => 'MYLABEL',
      'formatter' => 'collapsible',
      'instance_settings' => array(
        'description' => 'MYDESCRIPTION',
        'required_fields' => TRUE,
        'show_label' => TRUE,
        'label_element' => 'h2',
        'effect' => 'none',
        'speed' => 'fast',
        'classes' => 'group-MYCLASS field-group-div',
      ),
    ),
  );
  field_group_group_save($group);
}

And here is the code I used for deletion:

$group_name = 'MYGROUP';
$entity_type = 'node';
$bundle = 'MYNODETYPE';
$mode = 'form';
if ($group = field_group_load_field_group($group_name, $entity_type, $bundle, $mode)) {
  field_group_group_export_delete($group, FALSE);
}

Maybe it's just a documentation issue. At least I'm not the only one having it: One similar issue is described at https://www.drupal.org/node/1439010 and the same issue is described at http://agileadam.com/2014/01/creating-field-groups-in-a-custom-module/ (step 8).

Comments

leymannx created an issue. See original summary.

leymannx’s picture

Issue summary: View changes
leymannx’s picture

Issue summary: View changes
leymannx’s picture

Issue summary: View changes