3

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.

enter image description here

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.

enter image description here

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 ?

4 Answers 4

3

I've just been encountering a similar problem. And solved it like following.

In my MYMODULE.module file:

function MYMODULE_theme($existing, $type, $theme, $path) {

  return [
    'mythemename' => [
      'render element' => 'form',
      'template' => 'my-template-name',
    ],
  ];
}

In the $form:

$form['#theme'] = 'mythemename';

And finally in the my-template-name.html.twig template:

<div class="someclass">
  {{ form }}
</div>
0
0

The form element already has a base template.

To override the base template you will need to add twig file in your theme's templates directory.

The template directory in modules exists to provide base templates.

9
  • thanks for answer :) If I understand, I must add le suggest twig template in the theme's template directory (this can't work in module/templates dir). Can you detail what the template directory in module ? When you speak about "provide bas template", you mean I can only add in this dir bases templates (like form.html.twig by example?) Commented Sep 24, 2016 at 22:22
  • The template directory in modules exists to provide templates for new themes provided by the module. For example a template for a new type of field.
    – Eyal
    Commented Sep 24, 2016 at 22:25
  • Ok, I see...but in the tutirial I linked in my initial post, its seems we can use the suggest template in the custom/mymodule/templates) , in the part 'Look & feel of the form' #4.. Commented Sep 24, 2016 at 22:31
  • I could be wrong, but it looks like the tutorial is mistreating on this account.
    – Eyal
    Commented Sep 24, 2016 at 22:34
  • The reason modules don't support this behavior is that in the end, only one template can be used. If you had multiple modules with the same template file name, which of the two should drupal chose?
    – Eyal
    Commented Sep 24, 2016 at 22:37
0

I think you need to copy the content of form.html.twig file to your custom template. Then you can make further changes in your custom template. This way, your form should render on the page.

1
  • I thinked too :) I copied the content of the form.html.twik located from drupal/core/themes/classy/templates/form/form.html.twig to mymodule/templates/form--myformtest-form.twig (this is the code I showed in my initial post without the html h1 element). But the form doesn't show in my form page Commented Sep 25, 2016 at 10:55
0

This is the solution :

I don't understand why copy and past the default form twig content doesn't work. (why attributes and children variables doesn't work)

I dumped all availables twig variables from my custom template (via {{ dump() }} ) . I see I had a array named "element"..I tried this and its work ! the form is displayed.

This is the code of my custom form template :

<h1>TEST</h1>
<form{{ element }}></form>
2
  • 1
    It should be like <form{{ attributes }}>{{ children }}</form>.
    – Cool
    Commented May 20, 2017 at 8:23
  • This answer is accepted, but it is not good. At the very least, it should have been <form>{{ element }}</form> (notice the >. There is a chance that that will work, because element may contain the contents of the form. but that is not how it's intended to work.
    – commonpike
    Commented Jun 20, 2023 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.