Structure field update

Hi guys,

I’m trying to update a structure field but don’t how what format to pass it to the $page->update() function. My structure field has the following setup:

  poptions:
    label: Product options
    type: structure
    style: table
    fields:
      title:
        label: Title
        type: text
        required: true
      quantity:
        label: Quantity
        type: number
        required: true
      price:
        label: Price
        type: number
        step: .01

Now I’m getting a new product quantity through a webhook and managed to “find” the right field in the structure but I see no way updating it.
I tried updating the object I get back from ->toStructure(), updating it and passing it back but ->update() doesnt like the value.

$poptions = $project->poptions()->toStructure();
$poptions[$field['value']]['quantity'] = $quant3;
$project->update([
     'poptions' => $poptions
 ]);

Any hint would be great, Thank you!
Ben

Here’s an example function:

    /**
     * Add a new element to a kirby structure field
     * @param string $page
     * @param string $field
     * @param array $data
     */
    function addToStructure($page, $field, $data = array()){
      $fieldData = page($page)->$field()->yaml();
      $fieldData[] = $data;
      $fieldData = yaml::encode($fieldData);
      try {
        page($page)->update(array($field => $fieldData));
        return true;
      } catch(Exception $e) {
        return $e->getMessage();
      }
    }
2 Likes

Worked perfect thank you!