Dynamic structure blueprint

I’m writing a form builder plugin and to display the collected data of a form I need a dynamic structure. Here is an example to clarify what I mean:

A user creates a form with a first_name and a last_name field with the form builder plugin. Than this form can be used to collect data. The form builder plugin stores this data as entries in a structure field. To display this data in the panel I need to set the fields of the structure. But because the fields are created dynamically by the user I can’t write them in the blueprint directly. Is there a way to generate the fields for the structure dynamically? Something like this (where generateFields could be defined in the page controller):

form_entries:
  type: structure
  fields: {{ generateFields() }}

which would be resolved to the following the example above:

form_entries:
  type: structure
  fields:
    first_name:
      type: text
    last_name:
      type: text

@arnoson did You succeed? Looking for solution for same problem - how to dynamically create blueprint structure.

If you can access the page where the data is stored directly (e.g page(‘somepage’), you could create the blueprint dynamically:

2 Likes

Dynamically created page from received data with Page::create.
Problem is - that I can not see that page in panel, because it does not have appropriate blueprint.

Two points that I do not understand:

  1. how can I get received data in blueprint plugin (so I could iterate through data and create dynamic page fields).
  2. If amount of page fields varies - does it means that I need to create blueprint for each situation - survey3.yaml survey7.yaml etc

The problem with dynamic blueprints is that – as I wrote in the recipe and mentioned above – you don’t have access to the current page object. You can fetch a specific page via the page helper, but that doesn’t really make sense if you have multiple pages created programmatically.

So I have /controllers/upload.php which receives data when frontend sends data to /templates/upload.php which calls Page::create($params).

Got a bit confused - Page::create has props: “template” which actually is name of blueprint, but there is also prop “blueprint” which sets blueprint object? Was trying to define blueprint fields here… could not manage to work it.

What I want to achieve - in same place where I create page (with received data) would like before or after define and create blueprint for that page (with defined text fields for each received form input).

Normally my blueprint would look like this:

name:
  label: Name
  type: text
surname:
  label: Surname
  type: text
question1:
  label: Question 1
  type: text
question2:
  label: Question 2
  type: text
... etc

One user will answer on 3 questions, other user will answer on 7 questions. I could hardcode maximum possible question fields in blueprint, but instead want to create that blueprint dynamically in moment when receive data from frontend.

Wouldn’t it make more sense to store the answers in a structure field in this case, where the number of entries doesn’t matter?

@texnixe also thought about this as option B, will try this way. Thank You!