Unable to add a renderable with a #theme key to group

Created on 16 April 2024, 2 months ago

Problem/Motivation

I was trying to add a custom widget that basically doesn't produce any input but needs HTML and changes values of other fields via JS in hook_field_group_form_process_build_alter().
Modifying the #fieldgroups and #group_children properties of the element works only for form elements for some reason (having the #type key), and not all (tried the html_tag element but it was not moved to the group). Finally ended wrapping my #theme element into a fieldset type:

/**
 * Implements hook_field_group_form_process_build_alter().
 */
function X_field_group_form_process_build_alter(array &$element, FormStateInterface $form_state, &$complete_form) {
  // Move the new widget to the right place.
  if (!\array_key_exists('my_widget', $element)) {
    return;
  }
  // Modify children of the right group - actually the order doesn't make any difference as it's controlled by #weight parameter.
  $group = $element['#fieldgroups']['group'];
  $new_children = [];
  foreach ($group->children as $child) {
    if ($child === 'next_field') {
      $new_children[] = 'blueprint_section';
    }
    $new_children[] = $child;
  }
  $group->children = $new_children;
  $element['#group_children']['my_widget'] = 'group';
  $my_widget = $element['my_widget'];

  // For some strange reason we cannot have a non-element here.
  // What's more, html_tag element also doesn't work with field_group.
  $element['my_widget'] = [
    '#type' => 'fieldset',
    '#group' => 'group',
    'my_widget' => $my_widget,
    '#weight' => 4,
  ];
}

I'd like to understand why I couldn't use a normal renderable there and only form elements and fieldsets were moved to the right place in the form?

πŸ’¬ Support request
Status

Active

Version

3.0

Component

Miscellaneous

Created by

πŸ‡΅πŸ‡±Poland Graber

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

Comments & Activities

Production build 0.69.0 2024