Accessing nested array items from an JSON file in a select field


As described in this reference document I’m accessing options from a JSON file.

Here’s the JSON file

Using Kirby’s toObject field method in my block, I can’t access the JSON items in the nested array.

I can only see the productTitle item if I remove the toObject method.

Do I need to append something to the query?

Here’s my yaml blueprint:

name: Single Product Block with Button 
        label: Product Picker 
        type: select 
          type: api url: 
          query: rows 
          text: "{{ item.productTitle }}" 
          value: "{{ item.productTitle.slug }}" 

And my php file

<?php if($product = $block->productPicker()->toObject()): ?> 
<?php var_dump($product) ?> 
<a href="<?= $product->productLink() ?>">
<img src="<?= $product->imageLink() ?>" alt="<?= $product->productTitle() ?>" width="300" height="auto" /></a> 
<h1><?= $product->productTitle() ?></h1> 
<p><?= $product->price() ?></p> </div> 
<?php endif ?>

Very many thanks in advance!


This method is supposed to be called on a field of type object (yaml data stored in field), not on a select field. Not sure what you are trying to do here.

Hi Pixelijn, thank you.

Not sure what you are trying to do here.

Okay, let me try and give a bit of context.

The JSON file is a table of products, with title, price, URLs etc.

I want to use a select field in the panel to allow the editor to choose a product from that table.

That item will then populate a block php file with the values for each of the properties in the products table.

Ultimately, this project is an email newsletter builder – the page is the email, the editor wants to be able to select products from the catalogue to feature in the email. We take the page HTML and push it into our ESP. Once I have this basic set up working we’ll probably be connecting to a full Shopify product catalogue via a JSON feed from their store.

Well, but your select field stores the sluggified title of the product. So in your block, to get the other information for the product, you would have to query that API again to get the other information about the product.

So in the Block file, I should again read the contents of the JSON file, and somehow filter for the value stored in the select field?

Would I read the contents of the JSON file into a variable, and then do a toArray() method on that variable? Would the column headers be available as keys?

I’m learning this, so thank you for your help!

(I guess I probably should also add a UID to the products table)

You would json_decode() (PHP method) the json from the API into an array, then find the element by what is stored in your field. Using a UID would make that job a little easier than the sluggified title.

In addition to the above, it would make sense to cache the API response, to prevent unnecessary calls to the API all the time, depending on how often the catalog changes.