<p>I figured it out. You have to create a separate block and it somehow figures out it's a child based on the level you specify. So something like this:</p>
<pre><code>[
    'new1' = [
        'modified' =&gt; 1,
        'type' =&gt; 'parentBlock',
        'enabled' =&gt; 1,
        'collapsed' =&gt; 0,
        'level' =&gt; 0,
        'fields' =&gt; [
            'theme' =&gt; 'black',
        ],
    ],
    'new2'= [
        'modified' =&gt; 1,
        'type' =&gt; 'childBlock',
        'enabled' =&gt; 1,
        'collapsed' =&gt; 0,
        'level' =&gt; 1, // increment the level
        'fields' =&gt; [
            'value' =&gt; 'text',
        ],
    ]
]
</code></pre>

CC BY-SA 4.0
2021-05-06T11:44:56.173Z
2021-05-06T11:44:56.173Z
user11962208
<p>I already discovered how to create a neo block programmatically from this post: <a href="https://craftcms.stackexchange.com/questions/35451/how-can-i-set-a-neo-fields-values-from-my-plugin-module">How can I set a Neo field&#39;s values from my plugin/module?</a></p>
<p>However, I don't know how I can create a neo block with child blocks.</p>
<p>So the idea is I want something like this:</p>
<pre><code>[
    'blocks' =&gt; [ // Neo field handle
        'new0' =&gt; [
            'modified' =&gt; 1,
            'type' =&gt; 'topLevel', // Top level neo block
            'enabled' =&gt; 1,
            'collapsed' =&gt; 0,
            'level' =&gt; 0,
            'fields' =&gt; [
                'someField' =&gt; true, // Neo block field
            ],
            'children' =&gt; [
                'new0' =&gt; [
                    'modified' =&gt; 1,
                    'type' =&gt; 'secondLevel', // child neo block
                    'enabled' =&gt; 1,
                    'collapsed' =&gt; 0,
                    'level' =&gt; 0,
                ]
            ]
        ]
    ]
]
</code></pre>
<p>The <code>children</code> array is just what I imagine the solution might be like. But this doesn't work.</p>
<p>Does anybody know how it should be done?</p>
<p>Thanks</p>

<plugin-development><craft3><plugin-neo>
Programatically create a Neo block with children
# Programatically create a Neo block with children

(Asked by: username_0 on 2021-05-06)

I already discovered how to create a neo block programmatically from this post: How can I set a Neo field's values from my plugin/module?
However, I don't know how I can create a neo block with child blocks.
So the idea is I want something like this:
[
    'blocks' => [ // Neo field handle
        'new0' => [
            'modified' => 1,
            'type' => 'topLevel', // Top level neo block
            'enabled' => 1,
            'collapsed' => 0,
            'level' => 0,
            'fields' => [
                'someField' => true, // Neo block field
            ],
            'children' => [
                'new0' => [
                    'modified' => 1,
                    'type' => 'secondLevel', // child neo block
                    'enabled' => 1,
                    'collapsed' => 0,
                    'level' => 0,
                ]
            ]
        ]
    ]
]

The children array is just what I imagine the solution might be like. But this doesn't work.
Does anybody know how it should be done?
Thanks

**Tags:** plugin-development, craft3, plugin-neo


---

## 1 answers

### Answer by username_0 on 2021-05-06. Score: 3 | Accepted answer

I figured it out. You have to create a separate block and it somehow figures out it's a child based on the level you specify. So something like this:
[
    'new1' = [
        'modified' => 1,
        'type' => 'parentBlock',
        'enabled' => 1,
        'collapsed' => 0,
        'level' => 0,
        'fields' => [
            'theme' => 'black',
        ],
    ],
    'new2'= [
        'modified' => 1,
        'type' => 'childBlock',
        'enabled' => 1,
        'collapsed' => 0,
        'level' => 1, // increment the level
        'fields' => [
            'value' => 'text',
        ],
    ]
]


---

