1

Problem

I would like to have a theme suggestion for form elements (e.g. input, select, etc) that includes the form's ID. This would be nice for when I only want to make changes to the elements of that form and no other forms.

Currently I can use input.twig.html or input--textfield.twig.html for overriding a textfield. Therein lies the problem, neither of these theme suggestions are specific enough to only target the textfield for a specific form. I'm looking for the ability to have input--textfield--[FORM_ID].html.twig file.

Futhermore, it would be ideal to have a theme suggestion as follows input--[FIELD_NAME]--[FORM_ID].html.twig as this would allow a per field per form override.

Research

I have been unsuccessful in my research for finding a solution to this problem. Also, I am confused why this is not already in the theme suggestions (this is most likely to do my lack of knowledge on the twig system).

8
  • I hear what you're saying, and this may not be the ideal approach you are looking for, but I solved this another way drupal.stackexchange.com/questions/226491/… – Kevin Apr 7 '17 at 15:10
  • Yes, I did read your blog post about it and like you said it may not be ideal. If this isn't something that Drupal is providing by a default, then I'm at a loss as to why. It seems very limiting and that is something that Drupal usually doesn't do. – FatGuyLaughing Apr 7 '17 at 15:20
  • Like you said, there is a generic way to add more suggestions, but they wind up touching every element when in most cases you just want to touch one or two and leave the rest alone. – Kevin Apr 7 '17 at 15:20
  • I'm not sure how bad this idea would be but what about implementing hook_form_alter() to hit every form. Then loop over every element to create a twig file suggestion like you did but this would hit every form. I'm not sure what the impact would be. – FatGuyLaughing Apr 7 '17 at 15:24
  • One way to do it, sure. – Kevin Apr 7 '17 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.