I have a function set up in my App.php controller which I’m looking to pass to a Gutenberg block (its using sage-acf-wp-blocks), but unfortunately its showing an error:
"Undefined variable: testimonial_loop in on line : invalid argument supplied for foreach() in on line"
As screenshot shows, I’ve added the same function and outputted through a non-Gutenberg blade template which works fine. Any idea why the block may be showing this error? Thanks in advance for any advice on this.
My understanding is that block HTML is generated before any data even reaches your templates: You could treat your block HTML in your main blade template as essentially just a big string, not PHP that will be executing in the context of that template.
If the function isn’t needed outside the block, yes. If the function is needed elsewhere, I’d refactor it out of the controller: Then you can call the same function (and get the same logic and data) in the controller and easily pass it to the block filter.
So I refactored the query out of the controller as suggested which does indeed work within the new gutenberg block, but for some reason it refuses to show an ACF field ($testimonial_by) from a custom post type:
You aren’t giving this a second argument to tell it where to find the field. My guess would be that when the block blade executes it’s not in a context where ACF can derive an post ID to use to look up the field, so this returns false or null or whatever.
So eventually I got it all hooked up (after another slight bit of refactoring), so for anyone that may be having same issue, here is my final code for ref - I was able to echo the custom field with the get_the_id() argument to make it work successfully.