Add method to append to structure field

Hi @scottswany

I was also looking for a way to update the structure field type outside via the front-end.

Here’s how I did it:

// this is the page which has the structure field type (called submissions in my case)
$submissions = site()->find('submissions');
// get existing entries in an array
$existing_submissions_array = $submissions->content()->get('submissions')->yaml();
// add a new entry to the array
$existing_submissions_array[] = ['name' => $form['name'], 'email' => $form['email'];
// update the structure field type
page('submissions')->update([
   'submissions' => yaml::encode($existing_submissions_array)
]);

The $form variable holds data submitted through a front-end form. It’s coming from the uniform plugin (https://github.com/mzur/kirby-uniform).

The whole code is placed in a custom action for the uniform plugin but you can also place it in a controller. I prefer the plugin way because it’s more solid. It has built-in validation, spam filter, and a lot of flexibility.

Hope this helps.

Also, @Jan thanks so much for the original post!!

3 Likes