Panel Hook to Update Structure Keys

Hi there, I’m reworking my entire Kirby Invoicing project that I posted under “Interesting Use Cases” a while ago.

Planning an open source release at some point—particularly for freelancers wanting to take charge of their own finances :wink:

Question

I’m stuck on updating individual keys in structure fields with a hook plugin within the panel.

Example of Failed Attempt

Here is my failed attempt to update the sum key in the following example based on (quantity * rate).

// Structured field content (content/sales/invoice.txt)

Items: 

- 
  description: Cake
  quantity: "1"
  rate: "200"
  sum: ""
  date: ""
- 
  description: Biscuits
  quantity: "2"
  rate: "50"
  sum: ""
  date: ""

Concept for the hook I’m trying to set up.

// Hooks plugin (site/plugins/hooks.php)

$kirby->set('hook', 'panel.page.update', function ($page) {

		// Update item sum

		$items = $page->items()->toStructure();

		foreach ($items as $item) {

			$item = array()
				'quantity' => $item->quantity()->float(),
				'rate' => $item->rate()->float(),
				'sum' => $quantity * $rate,
			];

		}

		$items->update(array(
			'items' => yaml::encode($items),
		));

});


What am I missing? Would be grateful for any tips you might have.

Please do shoot me an email if you’d like to know more about the project!

Thanks, Oliver.

See this post: Updating individual entries in structure via AJAX

Fabulous!
Had completely missed that one, thanks @texnixe!