Score:0

Generating forms dynamically from json data

pt flag

I have a large set of JSON data which I need to process somehow into forms for users to interact with, fill in data and submit.

However, there are far too many to create all the forms by hand and I wonder if anyone can recommend a module/package which I can use to create such large number of forms dynamically.

Below is a very small sample section of json data, each section in the json needs to be processed into a unique step of a multipage form:

{
    "one":{
        "Label": "Please provide your age and date-of-birth",
        "fields": [
            "Age",
            "DOB"        
        ]
    },
    "two":{
        "Label": "Please provide your address",
        "fields": [
            "Address"        
        ]
    },
    "three":{
        "Label": "Please provide your 6 target areas",
        "fields": [
            "Area 1",
            "Area 2",
            "Area 3",
            "Area 4",
            "Area 5",
            "Area 6"
        ]
    }
}

As the data above shows, each of the forms have different number of fields. The labels will be textfield lables and the fields will be textfields.

Can anyone offer some suggestions or recommendations?

id flag
Although sometimes an answer contains a module recommendation, asking for that is against the forum rules: https://drupal.stackexchange.com/tour
apaderno avatar
us flag
I’m voting to close this question because it asks to find, recommend, or suggest hosting providers, tools, modules, themes, distributions, books, tutorials, documentation, or other off-site resources.
Score:1
cn flag

I think this is going to be tough, unfortunately.

The Feeds module has good JSON support, so that's a way to get the data in. The problem is: what will you import to? Webform would seem to be an obvious choice, but Feeds only has support for importing webform submissions, not the actual forms themselves.

Building it yourself

I have built forms using data stored on entities with the JSON Field module.

The basic approach is:

  • Create a custom content type or custom entity type (Custom Form Type).
  • Add a JSON field (My JSON Form) to Custom Form Type.
  • Import the JSON data to create the forms of Custom Form Type (using Feeds?).
  • Add a custom formatter (extending FormatterBase) to view the form field. (Optional. Users may not need to view the form field, and admins may be OK viewing raw JSON.)
  • Add a custom widget (extending StringTextareaWidget if you are using a JSON field) to add the drupal form fields.

How can users submit this form?

One way to do it:

  • Add a custom content type or custom entity type (Custom Form Submission).
  • Add a JSON field (My JSON Submission) to Custom Form Submission.
  • Add an entity reference field to type Custom Form Type.
  • Add logic to Custom Form Type that automatically creates a Custom Form Submission when submitted.

What about JSON validation? I built a validator with Opis JSON Schema and then run a check against my custom schema.

Reference

It might also be helpful to look at the JSON Field Utils module, which seems to be trying to re-implement paragraphs with JSON. This is not what you asked for, but it may give you ideas about how to code your own module.

pt flag
Thanks for your comment. Your initial comments about Feeds and Webforms is exactly what I explored earlier. I don't mind creating a custom module to achieve the desired results so your "Build it yourself" suggestion is very interesting - can you offer a bit more details on your form builds? I'm especially interested in knowing if in your build the submitted data was stored in the database via form fields ?
cn flag
@sisko Updated.
pt flag
Appreciated Patrick. I will begin looking into all your recommendations. Thanks.
pt flag
I have to give you credit and say thanks. I followed your approach and I am successfully able to use the Drupal Form API to auto generate forms dynamically from my imported JSON.
cn flag
@sisko Happy to help!
I sit in a Tesla and translated this thread with Ai:

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.