I have created an implementation of
hook_theme_suggestions_form_element_alter()
in my THEMENAME.theme file:
function manager_theme_suggestions_form_element_alter(array &$suggestions, array $variables) {
if (\Drupal::service('path.current')->getPath() == '/user/login') {
$suggestions[] = $variables['theme_hook_original'] . '--user-login';
}
}
Afterwards the template suggestion is correctly added to my suggested templates. I see that via my HTML markup in Chrome dev tools:

However, if this template file (file name form-element--user-login.html.twig) is existing in my theme's templates-folder, it is never loaded by Drupal. Only the template from
core/themes/classy/templates/form/form-element.html.twig
is being used. If I create a template file with the name "form-element.html.twig" inside my own theme's template folder, I can override the template from the base theme. Only my own template suggestion does not work.
Since
hook_theme_suggestions_form_element_alter()
seems to be the correct place to alter template file suggestions, I consider this a core bug.
Comments
Comment #2
jepster_Comment #3
jepster_Comment #4
jepster_Comment #5
jepster_I have figured out, how to work with the suggestions and the actual template. There need to be 2 functions.
1.,
Just informs about template suggestions. It does not load the templates.
E.g.:
2.,
Loads the template.
E.g.
Comment #6
jepster_Comment #8
ygstnd commented#5 works for me.
Comment #9
rromore commentedNote for future Googlers: you shouldn't need to define the `hook_preprocess_form_element` function. The `hook_theme_suggestions_form_element_alter` should use underscores instead of dashes for the suggestions array.
Comment #10
rick hood commentedI have similar problem
I can see the suggestion: form--views-exposed-form-search-global-page-1.html.twig ...and created that template file, but Drupal is not picking it up, still using: form.html.twig ...cleared cache a bunch.
Comment #11
volodymyr.oliinyk commented#9 worked for me
I used only hook_theme_suggestions_form_element_alter and inside
$suggestions[] = str_replace('-','_',[DYNAMICALLY GENERATED SUGGESTION]);
Comment #12
rahul_k commentedyou can also give a try for this module drupal.org/project/themable_forms
Comment #13
tsega commented@rrmore comment is correct at #9; one should not use dashes `-` in template suggestion names but rather underscores `_`. So the following works as expected.
Comment #14
isalmanhaider commentedYou can also use this handy module https://www.drupal.org/project/formdazzle
Comment #15
Steppico commentedI had the same problem with form-element. It didn't have any theme suggestions, thus my hook_theme() for a custom template did not work.
I ended up solving it this way.
In MYMODULE.module:
I've seen other more generic solutions trying to add to the $suggestions array by checking whether another $suggestion for that theme existed. In the case of the form-element, we know that it doesn't have any suggestion, thus such solution wouldn't work. BUT form-element does exist in the $hook parameter. It is a string, so by using strpos(), it is possible to verify whether form-element exists in the string, and if so, insert a new suggestion.
Finally, to make it work, it is just required to implement the hook_theme():
Hope it can help you achieve what you want.
Comment #16
martijn de witComment #17
drale01 commented#11 I don't know is it clear enough.
This was my code that didn't work:
But when I put this line of code
it works.
So it looks like this: