1

I'd like to know how to add a field to an existing fieldset in a user entity

I've seen this example:

Add Fields to Field Group programatically https://drupal.stackexchange.com/a/71902/1082

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

But I'm unsure what to supply for 'name' and 'bundle' and also what to supply for the line

$your_group = $groups['node']['article']['form']['group_your_group'];
2

I think you should try hook_form_alter. try this may it helpful to you.

function mymodule_form_alter(&$form, $form_state, $form_id) {

  // Check your form-ID 
  if ($form_id == 'user_profile_form') {
    // Creating custom field
    $form['new_field'] = array(
      '#weight' => 4,
      '#type' => 'textfield',
      '#title' => t('New Field Name'),
    );
  }
  // Attach to the existing group
  // Suppose group machine-name is group_general
  $form['#group_children']['new_field'] = 'group_general';
}
1

In case anyone stumbles upon this, here is what worked for me: in hook_form_alter,

 $form['#fieldgroups']['group_name']->children[] = 'field_name';

Where '#fieldgroups' is the actual name of the $form element, and 'group_name' is the machine name of your fieldset, and 'field_name' is the name of the field you want to insert.

This was for a commerce product form...not sure how it works attaching it to a user entity.

1

I used a different method based on your opening code:

$groups = field_group_read_groups(array(
 'entity_type' => 'node',
 'bundle'      => 'article',
 'mode'   => 'form'
));
$your_group = $groups['node']['article']['form']['my_group'];
$your_group->children[] = 'field_event';
field_group_group_save($your_group);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.