I'm working with an entity-creation form that needs some extensive theming. So far this has required me to alter template suggestions and create (twig) template variables for fieldsets. I've used template_preprocess_fieldset()
and hook_theme_suggestions_fieldset_alter()
to do this.
The suggestions and variables I'm creating are specific to one entity type, so I'd like to use the form id or the entity type to limit the action of the functions above, but the variables available within the scope of the functions don't include information outside the scope of the fieldsets themselves--in other words, I can't find out what form the fieldsets belong to.
Is there a way, in the context of a theme's preprocess_HOOK or HOOK_alter functions, to get information about the parent form?
I'd neglected to mention that I'd unsuccessfully tried @Berdir's two-part solution:
- Set some value in
hook_form_alter()
- Access that value in
hook_template_suggestions_fieldset_alter()
Going back at @Berdir's suggestion, I cracked it (thanks!) In Drupal 8, this combination works for my specific issue (and should work for other implementations of `hook_theme_suggestions_HOOK_alter() as well):
/**
* Implements hook_form_alter().
*/
function themename_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
switch ($form_id) {
case 'some_specific_form_id':
foreach ($form as $key => $value) {
if (strpos($key, 'field_') === 0) {
$form[$key]['widget']['#parent_form_id'] = $form_id;
}
}
break;
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function u2d8_theme_suggestions_fieldset_alter(array &$suggestions, array $variables) {
// Key the use of this suggestion to the front page.
if ($variables['element']['#parent_form_id'] === 'some_specific_form_id') {
$suggestions[] = 'fieldset__some_specific_form';
}
}