0

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?

2
  • 1
    Although sometimes an answer contains a module recommendation, asking for that is against the forum rules: drupal.stackexchange.com/tour
    – cilefen
    Nov 10, 2022 at 23:48
  • 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.
    – apaderno
    Nov 11, 2022 at 9:51

1 Answer 1

1

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.

5
  • 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 ?
    – sisko
    Nov 11, 2022 at 0:06
  • @sisko Updated. Nov 11, 2022 at 0:55
  • Appreciated Patrick. I will begin looking into all your recommendations. Thanks.
    – sisko
    Nov 11, 2022 at 20:30
  • 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.
    – sisko
    Nov 17, 2022 at 10:55
  • @sisko Happy to help! Nov 17, 2022 at 15:24

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