Panel hook update, add new structured YAML field

I put together this panel.page.update hook to add a structured field upon page save.

// hooks.php

<?php

$kirby->hook('panel.page.update', function ($page) {
  if ($page->intendedTemplate() === 'place') {

    // logic with $lat and $lng
    // {...}

    $location = array('lat' => $lat, 'lng' => $lng);

    try {
      $page->update(array(
        'location' => yaml::encode($location)
      ));
      $page->update(compact('address'));
    }
    catch(Exception $e) {
      echo $e->getMessage();
    }
  }
});

this add the following text to my page.txt:

Location: 

lat: "46.31436"
lng: "11.27232"

whereas it should be something like:

Location: 

-
  lat: "46.31436"
  lng: "11.27232"

What am I missing in the logic of hooks.php?

It’s not the logic, but the way your location array is built. It should be an array of arrays:

$location = array(
      array(
        'lat' => $lat,
        'lon' => $lon
      ));

Great, thank you.

v v