Get complete files objects data when converting fields to YAML through ->yaml() method

Hi,

  'hooks' => [
    'site.update:after' => function($newSite, $oldSite) {
      if (json_encode($newSite->mapData()->yaml()) != json_encode($oldSite->mapData()->yaml())) {

        // mapData is a structure field
        $locatedMapData = addLocationsData($newSite->mapData()->yaml(), $oldSite->mapData()->yaml());
        
        F::write('./assets/map-data.json', json_encode($locatedMapData['data']));
      }
    }
  ],

mapData is a structure field.

This script works well but the files data are saved as "cover":["file:\/\/QRaLeNkhnx5hlU53"], while I would like to save a complete file object (with name, src, alt etc.). Is it possible to convert them through the yaml() method ? Is it necessary to loop over each field ?

Thank you for your help.

No. The yaml (internally Data::decode($value, $method)) method converts the field value to an array. So yes, you would have to loop through each item in the resulting array and convert the value of the cover field to an array (or use PHP’s array methods).

1 Like

Clear, thank you.