Unable to programmatically add fieldset to an existing group

Created on 15 August 2022, over 2 years ago
Updated 11 November 2022, about 2 years ago

Code that worked in D8.9 no longer works in D9.2. I programmatically add an item (a "#markup" element) to an existing field group. By "existing", I mean that I configured groups in "Manage Form Display" for a Taxonomy field, then in HOOK_form_alter, I add the markup element to a group that already has two fieldsets in it. I attached a screenshot (form-config.png) of the form config.

The code that adds the markup element to the group is shown below:

  // Add a new column with only option labels to be rendered
  // to the left of the four QC settings fields.
  $form['qc_option_labels'] = [
    '#markup' => sunland_quality_control_get_qc_labels('option'),
    '#weight' => 0,
  ];

  $form['#fieldgroups']['group_quality_control']->children[] = 'qc_option_labels';
  $form['#group_children']['qc_option_labels'] = 'group_quality_control';

I added two more screenshots showing the resulting taxonomy form in D8.9 and D9.2. I shortened the list of fields involved to just three so everything would fit in the screenshot for D9.2. But there are actually 21 fields in the list, which is why I went to so much effort to make it more presentable and usable for the user.

πŸ› Bug report
Status

Closed: works as designed

Version

3.2

Component

Miscellaneous

Created by

πŸ‡ΊπŸ‡ΈUnited States ExTexan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States bob.hinrichs

    Drupal 10:
    I had a field named field_show_number_grantable that I added via form_alter, that was a toggle state field (didn't submit any value but was used on the front-end). I needed it within a field group with group_name of 'group_administrative'

    1) In my form_alter hook in the field_show_number_grantable array, add '#group' => 'group_administrative'
    2) add this function to my module:
    function MYMODULE_field_group_form_process(array &$element, &$group, &$complete_form) {
    if ($group->group_name == 'group_administrative') {
    $group->children[] = 'field_show_number_grantable';
    }
    }

Production build 0.71.5 2024