Hi, I’m currently testing Kirby Engineer Field by @jenstornell and it is quite brilliant so far. The blueprint and Panel part is going well, but I’m stuck when trying to access nested engineer fields.
Then I’m doing this in order to access field1 or field2 :
foreach($cards->toStructure() as $card)
{
//here I can access level 1 fields : $card->card_image()->toFile();
//but when I want to reach nested engineer fields :
foreach($card->content_blocks()->yaml() as $content_block)
{
var_dump($content_block['field1']);
// I get "Cannot use object of type Page as array"
}
}
The documentation only says :
Engineer saved the data similar to the structure field. toStructure
If you only use one level in depth and don’t use multiple fieldsets you should be fine using toStructure. yaml
If you use a more complex structure you can use yaml as a custom field method or the yaml method.
So, I guess I should use yaml for my nested fields, but I cannot seem to make it work. Any suggestions ?
Thanks !
Just a hunch but i don’t think you can mix them. Use one or the other. What happens if you do this:
foreach($cards->yaml() as $card)
{
//here I can access level 1 fields : $card->card_image()->toFile();
//but when I want to reach nested engineer fields :
foreach($card->content_blocks()->yaml() as $content_block)
{
var_dump($content_block['field1']);
// I get "Cannot use object of type Page as array"
}
}
Also try var_dump($content_block); which should give you the whole array and potentially a clue how to drill into it. Looks like your doing pretty much what the kirby docs say though.
My PHP skills are weak, but i hope this helps you.
Thanks for the suggestion !
Using Yaml seems to be the way to go. var_dump($content_block);gives a more sensible output now.
I feel that for the second foreach loop, it should be something more like
foreach($card[‘content_blocks’] as $content_block)
because $card is already an array, but I get a
Invalid argument supplied for foreach()
It will probably make more sense after a good night sleep. Thanks again for your input on this problem.
Edit: I forgot to check if $card['content_blocks'] existed before using it in a loop. Problem solved !