However, whenever I console chartConfiguration, it returns null. Context is not the issue. I’ve passed the post id of the page where I am currently testing this as a second argument in get_field().
I’ve successfully passed ACF Data to JS from a page template before but I cannot figure out how to do so with a block template.
Any tips on how to get around this that doesn’t include using ACF API? I’ve had no success getting it to work.
This is not the intent of a Controller. A Controller is for shaping/getting data before passing it to a Blade, not as a namespaced repository for related logic.
Have you already enqueued the make-a-chart script before localize_script() runs?
Have you passed the direct output of get_field() to localize_script() instead of wrapping it in a static method?
What action are you wrapping this call to localize_script() in? Some actions run before ACF has been fully initialized, and so ACF fields will return strange values.
I am guessing it’s somehow loading before ACF if I am still getting null here. I know there’s the ‘acf/init’ action but doesn’t all JS need to be enqueued in this action?
JS only needs to be enqueued before it’s printed out. wp-enqueue-scripts is just a useful place to do it. Have you looked at what’s being printed out on the page?
I think the issue is that the data is being held within the block and not the posts overall meta data. I ran parse_blocks($post->post_content) in my localize function and was able to find the data I need in the console.
I am guessing based on my research that there is no built-in method that easily extracts the block-level data eh?
I haven’t used the ACF block stuff very much. If the data is being stored in block attributes and not post meta then yeah, I’d guess that you need parse_blocks() to get at it (or something similar–I’m not familiar w/ the core block functions). If you were registering your own dynamic blocks, you could specify a PHP render callback that would have access to the block attributes, and could do what you want, but I’m not sure if you can do that w/ ACF’s blocks.