MWDelaney / sage-acf-wp-blocks

Composer library for generating ACF Gutenberg blocks from templates. Intended for use with Roots/Sage (http://roots.io)
346 stars 66 forks source link

Problem with $block['data'] and preview mode in Gutenberg #66

Closed hostis912 closed 2 years ago

hostis912 commented 2 years ago

Hello, I have a problem with $block['data'] in Gutenberg.

I use Sage 10 and WP 5.8.

When I want to insert a block in preview mode it is not loaded and I get in the console the message: POST https://xxxxx.test/wp/wp-admin/admin-ajax.php 500. After some time Gutenberg shows something about JSON error and the page crashes after a reload. But if you switch to edit mode, everything works without problems. After saving the page you can also switch to preview mode without problems.

Here is an excerpt from the block template:

...
 <h2>{{ $block['data']['lme__headline'] }}</h2>
...

If you output $block['data'] via console.log() for example on the web page, you get:

data:
lme__headline: "Latest Magazine Entries"
_lme__headline: "field_611bc02742f5e"

This fits so far, but in Gutenberg it looks like this only after inserting it via edit mode after you have saved the page. The first time you insert it in preview mode $block['data'] looks like this:

data:
_lme__headline: "field_611bc02742f5e"

If you use this in the template, there are no problems at all: <h2>{{ get_field('lme__headline') }}</h2>--}}

Why is lme__headline: "Latest Magazine Entries" initial missing in $block['data'] in Gutenberg?

Maybe you have a solution for this problem, otherwise I have to switch to get_field everywhere instead of $block['data']['xyz'].