Programmatically update specific structure field entry

Hello. I have a structure field that I’m using to save front-end form data. My structure field looks something like this…

  label: Form Entries 
  type: structure
      type: hidden
      label: Name
      type: text 
      label: Status 
      type: text

I’m successfully adding entries programmatically thanks to this forum post. I need to be able to update a previously saved entry. I’m saving a unique value in the entry_id field. I’m assuming I’ll need that to find the entry I need to update. In my case I need to update the entry_status of a previously saved entry.

Any insight would be greatly appreciated.

Since you can only ever update the complete field, you first get the structure field array like in the addtoStructure() method.

Then find the key to update and change the values you want to change. Then update the page with the new data. Here is an example (using the Starterkit about page with the social structure field):

$p    = page('about');
$data = $p->social()->yaml();


// in your example you would search for the entry_id column and value instead
$key  = array_search('Twitter', array_column($data, 'platform'));

// in your case you would leave the entry_id alone but change the other values as needed
if($key) {
  $data[$key]['platform'] = 'Birdy';


$p->update(['social' => Data::encode($data, 'yaml')]);

Exactly these problems I also always have. Therefore please Upvote for this idea :wink:

@texnixe, Thanks for your insight and helpful code example.

@Oli1, Having the ability to efficiently update entries would be most welcome. I went ahead and upvoted that feature request.