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();
      }
    }
4 Likes

Worked perfect thank you!

I’m trying use this function for saving uniform values, but I’m getting:

Kirby \ Exception \ InvalidArgumentException (error.invalidArgument)
Unexpected input
return $response->send($html);
        }
 
        // Files
        if (is_a($input, 'Kirby\Cms\File')) {
            return $response->redirect($input->mediaUrl(), 307)->send();
        }
 
        // Simple HTML response
        if (is_string($input) === true) {
            return $response->send($input);
        }
 
        // array to json conversion
        if (is_array($input) === true) {
            return $response->json($input)->send();
        }
 
        throw new InvalidArgumentException('Unexpected input');
    }
 
    /**
     * Renders a single KirbyTag with the given attributes
     *
     * @internal
     * @param string $type
     * @param string $value
     * @param array $attr
     * @param array $data
     * @return string
     */
    public function kirbytag(string $type, string $value = null, array $attr = [], array $data = []): string
    {
        $data['kirby']  = $data['kirby']  ?? $this;
        $data['site']   = $data['site']   ?? $data['kirby']->site();
        $data['parent'] = $data['parent'] ?? $data['site']->page();
 
        return (new KirbyTag($type, $value, $attr, $data, $this->options))->render();
    }

Structure field is updated, but I’m getting 500 and can’t do next stuff like show success form message. I’m using Uniform AJAX solution within route in config file.

Thanks for any suggestion

It looks like the problem was on my side. Sry

Another one :slight_smile:

On mac everything working as expected. Form was sended, emails was sended, success message wa showed and structure field was updated.
On win, form was ended, emails was sended, success message wa showed, but structure field is not updated.

Maybe problem within permissions? I don’t know…

Update:
This is not OS related issue.

Field is updated when I’m logged in.
So it looks like permission problem.

Any suggestion how to update addToStructure function?

Thanks

Sure you are on Kirby 2?