1

I am using the field_group module and want to programmatically create groups of fields in the hook_install of my module.install file.

It seems that field_group doesn't have much of an api that can be used by other modules. Does anyone know of a way that I can create a field group and assign field types to it programmatically?

4 Answers 4

3

The marked answer is correct here, but I can't comment on it. There's a couple "bugs" in it. The following changes were made to make it work with the object/array that gets exported when using field_group_load_field_group()

Specifically, I changed this line:

'format' => array('type' => 'fieldset'),

To:

'format' => array('type' => $group['format_type']),

And:

foreach ($group['fields'] as $field_name) {

To:

foreach ($group['children'] as $field_name) {

The final code:

/**
 * Create a field group.
 *
 * The field_group module does not come with an API that can be used by other
 * modules. This method fakes a Drupal new group form submission to create the
 * new group.
 *
 * @param  array  $group
 */
function _field_group_create_group(array $group) {

  drupal_load('module', 'field_group');
  module_load_include('inc', 'field_group', 'field_group.field_ui');

  $defaults = array(
    'entity_type' => 'node',
    'mode' => 'form',
    'parent' => '',
    // TODO: default to heaviest weight
    'weight' => '',
    'format' => array('type' => $group['format_type']),
    'fields' => array()
  );

  $group = $group + $defaults;

  $form = array(
    '#entity_type' => $group['entity_type'],
    '#bundle' => $group['bundle'],
    '#view_mode' => $group['mode'],
    '#groups' => array()
  );

  $form_state = array(
    'values' =>  array(
      'fields' => array(
        '_add_new_group' => array(
          'group_name' => $group['group_name'],
          'label' => $group['label'],
          'parent' => $group['parent'],
          'parent_wrapper' => array('hidden_name' => '_add_new_group'),
          'weight' => $group['weight'],
          'format' => $group['format'],
        )
      )
    )
  );

  foreach ($group['children'] as $field_name) {
    $form_state['values']['fields'][$field_name] = array(
      'parent' => '_add_new_group',
    );
  }

  field_group_field_overview_submit($form, $form_state);
}
1

I ended up rolling my own function to do this (works for me, YMMV):

/**
 * Create a field group.
 *
 * The field_group module does not come with an API that can be used by other
 * modules. This method fakes a Drupal new group form submission to create the
 * new group.
 *
 * @param  array  $group
 */
function _field_group_create_group(array $group) {

  drupal_load('module', 'field_group');
  module_load_include('inc', 'field_group', 'field_group.field_ui');

  $defaults = array(
    'entity_type' => 'node',
    'mode' => 'form',
    'parent' => '',
    // TODO: default to heaviest weight
    'weight' => '',
    'format' => array('type' => 'fieldset'),
    'fields' => array()
  );

  $group = $group + $defaults;

  $form = array(
    '#entity_type' => $group['entity_type'],
    '#bundle' => $group['bundle'],
    '#view_mode' => $group['mode'],
    '#groups' => array()
  );

  $form_state = array(
    'values' =>  array(
      'fields' => array(
        '_add_new_group' => array(
          'group_name' => $group['group_name'],
          'label' => $group['label'],
          'parent' => $group['parent'],
          'parent_wrapper' => array('hidden_name' => '_add_new_group'),
          'weight' => $group['weight'],
          'format' => $group['format'],
        )
      )
    )
  );

  foreach ($group['fields'] as $field_name) {
    $form_state['values']['fields'][$field_name] = array(
      'parent' => '_add_new_group',
    );
  }

  field_group_field_overview_submit($form, $form_state);
}
1

Check this out, all can be done this way: https://www.drupal.org/node/1425984

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);
    ctools_include('export');
    ctools_export_crud_enable('field_group', $group->identifier);
  }
}
1
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. Commented Mar 16, 2015 at 11:35
0

My solution - works for me

function mymodule_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);
}

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.