# Dynamic structure blueprint

**URL:** <https://forum.getkirby.com/t/dynamic-structure-blueprint/22426>\
**Category:** Questions\
**Tags:** v3\
**Created:** [May 26, 2021, 8:34pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426 "2021-05-26T20:34:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnoson](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/arnoson/32/6155_2.png) [@arnoson](https://forum.getkirby.com/u/arnoson)\
**Post date:** [May 26, 2021, 8:34pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/1 "2021-05-26T20:34:00Z")

</div>

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):

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

```

which would be resolved to the following the example above:

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

```

---

<div class="post-metadata">

**Author:** ![Bristolscript](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bristolscript/32/4740_2.png) [@Bristolscript](https://forum.getkirby.com/u/Bristolscript)\
**Post date:** [December 13, 2021, 1:16am UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/2 "2021-12-13T01:16:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2021, 6:47am UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/3 "2021-12-13T06:47:20Z")

</div>

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

- [Blueprints | Kirby CMS](https://getkirby.com/docs/reference/plugins/extensions/blueprints#callback-definition)
- [Add programmable blueprints recipe by texnixe · Pull Request #1569 · getkirby/getkirby.com · GitHub](https://github.com/getkirby/getkirby.com/pull/1569)

---

<div class="post-metadata">

**Author:** ![Bristolscript](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bristolscript/32/4740_2.png) [@Bristolscript](https://forum.getkirby.com/u/Bristolscript)\
**Post date:** [December 13, 2021, 11:01pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/4 "2021-12-13T23:01:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 13, 2021, 11:26pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/5 "2021-12-13T23:26:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bristolscript](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bristolscript/32/4740_2.png) [@Bristolscript](https://forum.getkirby.com/u/Bristolscript)\
**Post date:** [December 14, 2021, 12:13am UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/6 "2021-12-14T00:13:55Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 14, 2021, 9:35am UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/7 "2021-12-14T09:35:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Bristolscript](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bristolscript/32/4740_2.png) [@Bristolscript](https://forum.getkirby.com/u/Bristolscript)\
**Post date:** [December 16, 2021, 10:37am UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/8 "2021-12-16T10:37:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![illycz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/illycz/32/5190_2.png) [@illycz](https://forum.getkirby.com/u/illycz)\
**Post date:** [August 25, 2022, 10:27am UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/10 "2022-08-25T10:27:18Z")

</div>

I have blueprint like @arnoson where I can define form fields like structure items and I wan’t define submissions fields for this dynamic form fields. I understand correctly that this is not possible?

Thanks

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 25, 2022, 10:34am UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/11 "2022-08-25T10:34:04Z")

</div>

Look into programmable blueprints: [Programmable blueprints | Kirby CMS](https://getkirby.com/docs/cookbook/panel/programmable-blueprints)

Should work fine for this use case.

---

<div class="post-metadata">

**Author:** ![illycz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/illycz/32/5190_2.png) [@illycz](https://forum.getkirby.com/u/illycz)\
**Post date:** [August 25, 2022, 6:41pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/12 "2022-08-25T18:41:41Z")

</div>

But I need reuse blueprint for multiple pages. How can I get actual form fields for submissions fields definition if current page is not accessible? Thanks

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 25, 2022, 7:00pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/13 "2022-08-25T19:00:03Z")

</div>

> [@illycz](#):
>
> How can I get actual form fields for submissions fields definition if current page is not accessible?

Oh, ok, then this approach doesn’t work. Maybe store it in some other format instead, JSON for example.

---

<div class="post-metadata">

**Author:** ![illycz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/illycz/32/5190_2.png) [@illycz](https://forum.getkirby.com/u/illycz)\
**Post date:** [August 25, 2022, 8:23pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/14 "2022-08-25T20:23:04Z")

</div>

And how can I show this type of submission format in panel? Thanks

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 25, 2022, 8:36pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/15 "2022-08-25T20:36:52Z")

</div>

Just as Information or editable?

---

<div class="post-metadata">

**Author:** ![illycz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/illycz/32/5190_2.png) [@illycz](https://forum.getkirby.com/u/illycz)\
**Post date:** [August 25, 2022, 8:40pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/16 "2022-08-25T20:40:33Z")

</div>

Just as information. And exportable would be perfect 🙂

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 25, 2022, 8:53pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/17 "2022-08-25T20:53:35Z")

</div>

Just as information you could output the content of the stored json in an info field (via a custom page method that outputs some html).

With JSON, you can export as anything.

---

<div class="post-metadata">

**Author:** ![illycz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/illycz/32/5190_2.png) [@illycz](https://forum.getkirby.com/u/illycz)\
**Post date:** [August 25, 2022, 9:16pm UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/18 "2022-08-25T21:16:46Z")

</div>

Can you share some links to detailed informations about that? Thanks!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 26, 2022, 6:03am UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/19 "2022-08-26T06:03:46Z")

</div>

Info field: [Info | Kirby CMS](https://getkirby.com/docs/reference/panel/fields/info)  
Field method: [Field methods | Kirby CMS](https://getkirby.com/docs/reference/plugins/extensions/field-methods)

In your info field you would do something like this:

```auto
fields:
  info:
  type: info
  theme: neutral
  text: "{< page.jsonFieldName.toText >}"

```

In your `toText()` custom field method, you would generate the output for the field.

---

<div class="post-metadata">

**Author:** ![illycz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/illycz/32/5190_2.png) [@illycz](https://forum.getkirby.com/u/illycz)\
**Post date:** [August 26, 2022, 9:20am UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/20 "2022-08-26T09:20:45Z")

</div>

OK, so if I’m understand correctly.

1. I need to save form submission to predefined page field in json format (some link for this would be perfect 🙂
2. Define custom field method which transform json to html
3. Define info field where I can output submissions field as html

Correct?

---

<div class="post-metadata">

**Author:** ![illycz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/illycz/32/5190_2.png) [@illycz](https://forum.getkirby.com/u/illycz)\
**Post date:** [August 26, 2022, 9:25am UTC](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426/21 "2022-08-26T09:25:04Z")

</div>

I have this for saving form data to structure field:

```auto
function addToStructure($page, $field, $data = array()){
    $kirby = kirby();
    $kirby->impersonate('kirby');

    $fieldData = page($page)->$field()->yaml();
    $fieldData[] = $data;
    $fieldData = yaml::encode($fieldData);

    try {
        page($page)->update(array($field => $fieldData));
        return true;
    } catch(Exception $e) {
        return $e->getMessage();
    }
}

```

[Next page](https://forum.getkirby.com/t/dynamic-structure-blueprint/22426.md?page=2)
