I want to add much HTML to my form elements in order to arrange them on the page, I will be using bootstrap framework to do this.

Which method would be better, to create a twig template file or to use HOOK_form_alter?

If the way to do this would be to create a twig template file, then how can I ensure the form html tags with the form method is also rendered to the page? I have attempted to do this but I don't know how to get the actual form to render, just the inputs are there in the html source code.

I know in Drupal 7 that you had to render something called child elements, but when I check for this array in Kint there is no such thing.


A good starting point is the bootstrap theme. They have a stable version for drupal 8. (It's the most object oriented theme I've seen so far)

So I would suggest to make your custom theme, as sub-theme of it.

If you want to do it fully by yourself, you would use hook_form alter existing forms (if needed) and theme_hooks/html.twig to add some bootstrap specific classes and change HTML structure. The mentioned theme is a good starting point to look at all the things that need to be changed.

  • Ok thanks, so I will use Hook_form_alter to add classes to the already existing html? Can this be done in the MYTHEME.theme file? – user43161 Sep 13 '16 at 5:27
  • I really would suggest you use the bootstrap theme. (or at least check out their code for reuse) hook_form_(FORM_ID)_alter is normally used in a custom module to alter existing form. theme_hook (like theme_checkbox) is normally placed in the .theme file you mentioned, and is the way also to add classes and change the HTML structure for all those elements. – rémy Sep 13 '16 at 7:57


That gives some code to add to the template.php file for a login form and could be a useful base for you to improve upon.

I'm doing the exact same thing as you are right now.

Your Answer

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