I would like to create a twig template for the form displayed inside the paragraph widget.

The only way I found is:

function mymodule_field_widget_paragraphs_form_alter(&$element, &$form_state, $context) {
// Check that the subform is opened  
if (count($element['subform']) > 2) {
    $element['subform']['#theme'][] = 'form__aw_calculator';

After that, I have created a twig template form--aw-calculator.html.twig like the following

<form{{ attributes }}>
    <div class="grid--all three--columns">
        <div class="column column--first bg--red-level-3">
            {{ element.field_calc_ts }}
            {{ element.field_calc_ts_greenlight }}
            {{ element.field_calc_budget }}

Unfortunately, this is half working... some of the fields are displayed some other are not (the form mode used in the paragraph widget setting should display all of them) More over, I can't see the markup coming from my twig....

I have the feeling that this is not the right way to handle this... any idea ?

  • What you are trying to do here will not really work, you can't free-style admin forms like the frontend. Even though admin themes use Twig files, it is largely controlled by form API, which enforces a very specific markup and very specific attributes to work properly. Altering the HTML markup will sooner or later break things, because contrib modules rely on that specific markup, specific attributes and hidden inputs. If you need to modify a form beyond CSS, better use Form API instead of Twig
    – Hudri
    Feb 23, 2022 at 15:35
  • I was using Inline entity form, and I was able to create a twig template for my form. Looks like this is not the case for the paragraph widget
    – Baud
    Feb 23, 2022 at 16:03
  • I have tried with Form API, this is working but not when a new paragraph is added. This is because the paragraph subform doesn't contains the fields during the time of the widget_type_form_alter hook. This is a nightmare
    – Baud
    Feb 23, 2022 at 17:57
  • @Baud, adding #theme is the right approach, but not a form__ template. Any other custom template name would do the job. See drupal.stackexchange.com/questions/249856/…
    – 4uk4
    Feb 23, 2022 at 20:21


Your Answer

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

Browse other questions tagged or ask your own question.