Log1x / acf-composer

Compose ACF Fields, Blocks, Widgets, and Option Pages with ACF Builder on Sage 10.
https://github.com/Log1x/acf-composer
MIT License
413 stars 56 forks source link

Using enqueue to run a localized script #143

Closed kupoback closed 1 year ago

kupoback commented 1 year ago

So I am looking to build a block that will support something like React or Vue. I see you have the enqueue method here, which executes the Sage 10 bundle code, however, I noticed that any data I need from this block is not passable. I am wondering if there is something I need to do differently, or another way to approach this.

Basically in Sage 9, what I do in the Controller file is just run wp_enqueue_scripts passing in the get_field functions to the array data. Since I'm using gutenberg blocks, I'm not 100% familiar with executing this, or if this package allows the support of this.

This is what my enqueue block looks like, and the post_id is being set as such due to it being a page declared to "page_for_posts". I can get the values on the front end but just not these localize values.

public function enqueue()
    :void
    {
        bundle('vue')
            ->localize(
                'BLOG',
                [
                       'labels'     => get_field('post_labels', get_option('page_for_posts')) ?? [], // has values
                       'no_results' => get_field('no_results', get_option('page_for_posts')) ?? '', // has value
                   ],
            );
    }

This is what the frontend returns:

<script id="vue/0-js-extra">var BLOG = {"labels":[],"no_results":""};</script>
kupoback commented 1 year ago

Seems that no matter what I do to make the DB call for the get_field, it will always return either null or default to my final ternary expression.

Log1x commented 1 year ago

The get_field() stuff should mostly be fixed from #163

The remaining stuff will get fixed when #154 is merged in.

Closing this and keeping #144 open.