Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Is it possible to add programmatically a field group?
Thank you
Is it possible to add programmatically a field group?
Thank you
Comments
Comment #1
Stalski CreditAttribution: Stalski commentedYes.
If you mean programmatically add a new field group type. See field_group.api.php
If you mean having field groups in code, you can have default fieldgroups in a hook (hook_field_group_info) or through features.
If you mean, having one at runtime, then no, but that would be a theming concept.
Comment #2
jlaurin CreditAttribution: jlaurin commentedThank you for your quick answer
Comment #3
jaimealsilva CreditAttribution: jaimealsilva commentedI can see hook_field_group_info() on field_group.api.php but it does not explain what the hook must return.
How do I describe/create the groups and assign them to entities?
How do I nest groups ?
How do I describe which fields go in each group?
Thanks.
Comment #4
jaimealsilva CreditAttribution: jaimealsilva commentedSorry to repeat, forgot to set the right version.
I can see hook_field_group_info() on field_group.api.php but it does not explain what the hook must return.
How do I describe/create the groups and assign them to entities?
How do I nest groups ?
How do I describe which fields go in each group?
Thanks.
Comment #5
Tengu CreditAttribution: Tengu commentedSubscribing, some examples for hook_field_group_info() would be really nice.
Comment #7
adoo CreditAttribution: adoo commentedDemo for groups of "webform" content type
/**
* Implements hook_field_group_info().
*/
function custom_module_field_group_info() {
$export = array();
$field_group = new stdClass();
$field_group->disabled = FALSE; /* Edit this to true to make a default field_group disabled initially */
$field_group->api_version = 1;
$field_group->identifier = 'group_NAME_OF_GROUP|node|webform|form';
$field_group->group_name = 'group_NAME_OF_GROUP';
$field_group->entity_type = 'node';
$field_group->bundle = 'webform';
$field_group->mode = 'form';
$field_group->parent_name = '';
$field_group->data = array(
'label' => 'NAME OF GROUP',
'weight' => '1',
'children' => array(
0 => 'field_YOUR_FIELD',
),
'format_type' => 'fieldset',
'format_settings' => array(
'formatter' => 'collapsible',
'instance_settings' => array(
'description' => '',
'classes' => '',
'required_fields' => 1,
),
),
);
$export['group_NAME_OF_GROUP|node|webform|form'] = $field_group;
return $export;
}
Comment #8
clivesj CreditAttribution: clivesj commentedI used the code from #7 (in mymodule.module) but the hook is not called.
The groups are not created and there is no entry in the database table field_group.
Do I have to invoke the hook manually?
Thanks
Comment #9
haggins CreditAttribution: haggins commentedYou also need to implement hook_ctools_plugin_api():
Comment #10
nils.destoop CreditAttribution: nils.destoop commentedLatest comment should idd fix the problem.