I have developed a custom module that creates a new content type with several fields. I would like some of those fields to be automatically moved into field groups (tabs, fieldsets, accordion, etc). Is there a way to obtain this through code, in such a way that the configuration set by code can then be changed in the field management page as with groups created directly in the UI?

Comments

spydmobile’s picture

I also am looking for a snippet of code to demonstrate this.

spydmobile’s picture

I found a perfect example that really helped here:
http://drupal.org/node/1142344

Norberto Ostallo’s picture

Thanks, that is exactly what I was looking for. In my code I was missing the hook_ctools_plugin_api() implementation.

jonhattan’s picture

Status: Active » Fixed

Ctools is not needed to create a fieldgroup.

What I do is creating a field group via web and then dump the raw object by running the following via devel's php block.

$group = field_group_load_field_group('group_attributes', 'node', 'article', 'form');
dvm($group);

Then, this is the code I put in my hook_install():

  $group = (object) array(
    'identifier' => 'group_attributes|node|article|form',
    'group_name' => 'group_attributes',
    'entity_type' => 'node',
    'bundle' => 'article',
    'mode' => 'form',
    'label' => 'Attributes',
    'children' => array(),
    'weight' => '7',
    'format_type' => 'fieldset',
    'format_settings' => array(
      'formatter' => 'collapsible',
      'instance_settings' => array(
        'description' => '',
        'classes' => '',
        'required_fields' => 1,
      ),
    ),
  );
  field_group_group_save($group);

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

haggins’s picture

Status: Closed (fixed) » Active

I'm trying the way jonhattan suggested and I see the field group in the database. However, the group is not visible on the entitys field managment page.

Am I missing something?

function mymodule_install() {
// Add fieldgroup:
  $group_name = 'group_bankverbindung';
  $entity_type = 'profile2';
  $bundle = 'organizer_data';
  $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' => 'Bankverbindung',
      'weight' => '1',
      'children' => array(
        'field_foobar',
      ),
      'format_type' => 'fieldset',
      'format_settings' => array(
        'formatter' => 'collapsible',
        'instance_settings' => array(
          'description' => '',
          'classes' => '',
          'required_fields' => 1,
        ),
      ),
    );
    field_group_group_save($group);
  }
}
jonhattan’s picture

I'm using the method explained extensively with no problem. Only difference I see with your code is that I'm using this for content types, not for profile2. Perhaps there's a difference.

bsandor’s picture

Issue summary: View changes

I have the same issue as @haggings in #6:
I can see the group in the database and not on my user entity.

I also got following error message during install:
Fatal error: __clone method called on non-object in .../sites/all/modules/field_group/field_group.module on line 1711

tadityar’s picture

does this applies for the Drupal 8 version?

Ivan Zugec’s picture

I had the same problem. I could see the group in the database but not on the manage field page. I had to run the following code to enable the group after field_group_group_save().

field_group_group_save($field_group);
ctools_include('export');
ctools_export_crud_enable('field_group', $group->identifier);
JayKandari’s picture

#4 worked for me.

jimmyko’s picture

Thanks @Ivan Zugec, #10 save my time!

akozoriz’s picture

None of this solutions don't work for me (Drupal 7.54). My solution for hook_install in custom module.
For the first (#4) get field_group object with php code (You can use /devel/php link in this case - need enabled Devel module)
Then put code to hook_install

//Field_group object
 $group = (object) array(
    'identifier' => 'group_attributes|node|article|form',
    'group_name' => 'group_attributes',
    'entity_type' => 'node',
    'bundle' => 'story',
    'mode' => 'form',
    'label' => 'Attributes',
    'children' => array(),
    'weight' => '7',
    'format_type' => 'fieldset',
    'format_settings' => array(
      'formatter' => 'collapsible',
      'instance_settings' => array(
        'description' => '',
        'classes' => '',
        'required_fields' => 1,
      ),
    ),
  );
    module_load_include('module', 'field_group');
    module_load_include('module', 'ctools');
    ctools_include('export');
 $new = new stdClass();
        $new->group_name = $group->group_name;
        $new->entity_type = $group->entity_type;
        $new->bundle = $group->bundle;
        $new->label = $group->label;
        $new->parent_name = $group->parent_name;
        $new->children =$group->children;
         // The form.
        $new->id = NULL;
        $new->weight = $group->weight;
        $new->mode = 'form';
        $new->format_type = $group->format_type;
        $new->format_settings = $group->format_settings;
        $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
        ctools_export_crud_save('field_group', $new);
ram4nd’s picture

How about when you want to update an existing one? Than you #10 and #4.

hodba’s picture

#14 worked for me, others didn't. thank you @akozoriz

rajesh.vishwakarma’s picture

Only #13 worked for me.