I generate a simple module "myformtest" with a simple Class form "MyformtestForm". I want to use a different theme for the form.
I followed this great tutorial. I added a "_theme_suggestions_alter" hook in my "myformtest.module", I activated twig debugging and I loaded the form page.
My "suggestion theme" is present and its name is "form--myformtest-form.html.twig". Then in my module directory:
- I added a sub-directory "templates"
- I added the twig file "form--myformtest-form.html.twig" in the sub-directory "drupal/modules/custom/myformtest/templates"
In the twig file, I added this simple code. (I copied it from the drupal/core/themes/classy/templates/form/form.html.twig and added the h1 html element for override)
<h1>TEST</h1>
<form{{ attributes }}>
{{ children }}
</form>
I clear caches (drush cr) and I reloaded my form page. But the html element h1 ("TEST") doesn't appear! Where is my mistake?
UP
I did it ! I added the hook theme in my .module file :
function myformtest_theme($existing, $type, $theme, $path)
{
return array(
'form__myformtest_form' => array(
'render element' => 'form',
'template' => 'form--myformtest-form',
),
);
}
But.... ! When I load my form page, the html element h1 "test" is displayed but not the form :/ I thinked my .twig file override the form.html.twig default theme.
Did I miss something ? Why the form doesn't displayed, how can I fix it ? And how can I pass a custom array from my build method of form Class to my twig template ?