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.
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.