Problem/Motivation
The functionality has been added in #3171458: Media Library and Field groups not compatible to render field groups correctly on media library upload forms.
Sadly it doesn't work totally correct. ONLY if the view_mode "media_library" is explicitly used / enabled for the media type, the field groups are rendered. It doesn't fall back to "default", if that's not the case.
TL;DR;
The fallback to "default" view mode from "media_library" isn't working which leads to missing fieldgroups in that case.
This is the code that was added to field_group.module in the referenced issue:
/**
* Implements hook_form_media_library_add_form_upload_alter().
*/
function field_group_form_media_library_add_form_upload_alter(&$form, FormStateInterface $form_state) {
// Attach the fieldgroups to the media entity form in Media Library widget.
$storage = $form_state->getStorage();
if (!empty($storage['media'])) {
foreach ($storage['media'] as $delta => $media) {
$context = [
'entity_type' => $storage['media'][$delta]->getEntityTypeId(),
'bundle' => $storage['media'][$delta]->bundle(),
'entity' => $storage['media'][$delta],
'context' => 'form',
'display_context' => 'form',
'mode' => 'media_library',
];
field_group_attach_groups($form['media'][$delta]['fields'], $context);
$form['media'][$delta]['fields']['#process'][] = [FormatterHelper::class, 'formProcess'];
}
}
}
If I change 'mode' => 'media_library',
to 'mode' => 'default',
the fieldgroups are rendered correctly!
Expected behavior would be that mode falls back to "default", if view mode "media_library" isn't defined.
I guess this fallback has to be implemented in field_group_attach_groups()
and not in the concrete implementation?
As quickfix we could determine in the hook, if view mode 'media_library' is used and otherwise set 'mode' => 'default',
but that doesn't seem clean to me.
Comments