There is an option to update the page via frontend with $page->update(). This is great and works good.
In my case I use it to store signups from users for a birtdayparty into a structure-field. To add an item to the structurefield I have to get the whole field first, convert it to an array, appaned the new data to the array, convert back to yaml and then update the page.
it would be great to have something like:
$page->mystructure()->add(array( key => value));
(It would be even nicer, if the structure-field could be more like a database with something like add(), remove(), update() including something like
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
'submissions' => yaml::encode($existing_submissions_array)
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.