Score:0

How to theme a paragraph form

sa flag

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 ?

ru flag
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](https://api.drupal.org/api/drupal/elements/9.3.x) instead of Twig
Baud avatar
sa flag
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 avatar
sa flag
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
4uk4 avatar
cn flag
@Baud, adding `#theme` is the right approach, but not a `form__` template. Any other custom template name would do the job. See https://drupal.stackexchange.com/questions/249856/custom-registration-twig-template
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.