Change Structure Field Value from Frontend

i am using a complexe structure field, where i need to update some inline fields via the frontend. I found the very helpfull “addtoStructure” Snippet in the web, but now i need to update an existing field.

Example: I have articles with a specific quantity. Now i need to update the quantity of one article after a form post.
The Quantity of the article is stored in a strcture field.

Is there any way to do so?

This is the addToStructure function i am using, but i can only be used to create a new entry, or am i wrong?

function addToStructure($p, $field, $data = array()) {
  $fieldData = $p->$field()->yaml();
  $fieldData[] = $data;
  $fieldData = yaml::encode($fieldData);
  $p->update(array(
    $field => $fieldData
  ));
}

See this solution: Updating individual entries in structure via AJAX

Note that you must need a unique field to search by…

Thanks for the answer! I got it working but there was no need for ajax.

Inside a loop i am searching for the correct ID of the article and then i use this to update the structure:

$articlesPage = $pages->findBy('uid', 'artikel');
$articlesList = $articlesPage->articleStructure()->yaml();

            foreach($articlesList as &$article) {

                if($article['articleid'] == $id[$a]) {

                    $article['articleammount'] = $article['articleammount'] - 1;

                    $articlesPage->update(['articleStructure' => yaml::encode($articlesList)]);
                }
            }
1 Like

Yes, I didn’t mean to use Ajax, I just pointed to that thread for updating the structure field.

However, to make your code more secure, you should check if $articlesPage really is a page.