Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using enqueue to run a localized script #143

Closed
kupoback opened this issue Nov 29, 2022 · 2 comments
Closed

Using enqueue to run a localized script #143

kupoback opened this issue Nov 29, 2022 · 2 comments

Comments

@kupoback
Copy link
Contributor

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
Copy link
Contributor Author

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.

codepuncher added a commit to codepuncher/acf-composer that referenced this issue Dec 21, 2022
Ref Log1x#144 and Log1x#143 

I have played about locally on this and seems we can simply pass the `$block` array to the `enqueue` method which gives access to `$block['data']['my_field_name']`.
This allowed me to conditionally enqueue assets depending on field values.

This PR needs more work like adding the argument to `Block::enqueue($data = [])` but I opened the PR just to get the ball rolling on this.
It would result in a breaking change since the signature has changed.

@Log1x - anything to add to this? Not as nice as `get_field` in the method but hey it works.
@Log1x
Copy link
Owner

Log1x commented Jul 28, 2023

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.

@Log1x Log1x closed this as completed Jul 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants