Structure field items within front-end form

I have a form on the front-end that allows users to create a page by uploading files and inputting text to a variety of fields. I have this mostly working however I would like to include a ‘videos’ field that allows the user to upload a youtube link and caption which is the same as the field in the panel

I’m creating a draft page like so in the controller

 $person = page('people')->createChild([
    'slug'     => str::slug($data['title']),
    'template' => 'person',
    'content'  => $data

and my data is being passed like so

$data = [
   'title'    => get('name'),
   'videos' => get('videos')

my issue is how do I convert content from fields submitted as a series of structure items? I thought I could mimic the text formatting of what’s written in the text file when I do this manually in the panel and add it that way but I’m not having much luck with that approach.

Any help or pointers are much appreciated.

Check out this post with the addToStructure() method: Add to Structure in specific language

You will have to adapt the code an pass the yaml encode array to your data variable instead of updating inside the function, but you should get the idea how to fetch the original content of the field and add the new entries.

Or maybe I got this wrong and you want to create a sort of structure field in the frontend?

Thanks @texnixe. So I got this working sorta.

What I have is a textarea on the front end for videos with instructions to format the first line in the textarea as the video link, the second as a caption, then two line breaks followed by the second video link and so on…

Within the controller, I am then chopping this supplied info up and formatting it to match the expected structure field output…

  caption: >
    Some text for the caption
  caption: >
    Some text for the second video caption

I’m then saving that value into $data['videos'] and passing that with the rest of the $data as the content param with createChild and it does seem to submit and add all the various structure items as I can see and edit it the panel afterwards as expected.

Is there anything inherently wrong with that approach or have I just made it difficult for myself?

I’d store the chopped data into an array, merge that with the data that is already stored, then pass the yaml-encodde array to your $data variable.

1 Like