Skip to main content

Drupal 8/9 - How to add Twig Template Suggestions for fieldset Elements

This is  how you use the hook suggestions function (hook_theme_suggestions_fieldset_alter()) for fieldset  elements to add Twig Template Suggestions in drupal 8-9.

/**
 * @param array $suggestions
 * @param array $variables
 * @param $hook
 */
function THEMENAME_theme_suggestions_fieldset_alter(array &$suggestions, array $variables, $hook)
{
    if (isset($variables['element']['#id'])) {
        $id = str_replace("-", "_", $variables['element']['#id']);
        $suggestions[] = $hook . '__' . $id;
    }
}
best practices