-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Execute filter once on block registration #5006
Comments
@JasonHoffmann I'd love to get your thoughts on #5012 ? We're talking about communication between blocks, and making sure the current APIs include all of these kinds of needs. I think if you need to share a variable between blocks, a better pattern may be wp.data(). I think the quick fix, if you're going to use I wonder, if registering a store on wp.data that your blocks or other blocks can access and you can inject into components, is a better pattern for this use, or if you need something else:
I appreciate your thoughts on if this would work, or what else plugin devs like us need. |
@JasonHoffmann, it would be much easier to answer your question when I could see full code, but let me try to debug what could happen.
I assume you have those code inside // outside of edit
import { once } from 'lodash';
const getMyArray = () => {
return wp.hooks.applyFilters( 'blocks.customBlock.filterArray', [ 'option 1', 'option 2' ] );
};
// inside edit
const myArray = getMyArray(); It will be executed for the first time when your block renders, so all other plugins should have enough time to register their own modifications.
This code is valid and should append
Yes, that is expected when your plugin loads first before other plugins have a chance to register their filters. This is because filters are applied and stored in the variable at the time when your file is loaded and parsed by the browser. This is expected behavior when using JS modules.
I don't think we need it at the moment. All blocks are registered when editor instance gets loaded, so there is plenty of time for all plugins to register their own hooks. We can revisit that later if we discover that it blocks something for plugin developers, but so far it wasn't the case. I hope that you will be able to make your example work with the explanations I shared above. Let us know how it goes. |
If you want to share data between a couple of components, then definitely I will close this one as there are no actionable items for us. Feel free to reopen if you have more questions or I missed something. Always happy to help 👍 |
Issue Overview
When creating a custom block, I have a variable that I want to make available for filtering in case other developers want to customize the functionality further. The data I am using is an array that is used to populate a dropdown menu. I know for that to work I can add something like:
And that would allow another developer to add something like:
Which all works together fine in theory. However, I do not know where to put the
applyFilters
code in my call. If I add it to myedit
function, then it is executed at least twice on page load, and more if there are multiple blocks of the same kind. The same goes for thesave
function. So my array ends up looking something like:If I try to add the above code at the very top of a code block, before my
registerBlockType
function is even called, then it doesn't load at all since (I think)wp.hooks
hasn't loaded yet.Is there a way for me to execute the above to apply a filter to a variable just once preferably when a block is executed or registered initially? I don't think there's something like
init
in theregisterBlockType
object, but I wanted to see if I was missing something.Happy to provide more details if necessary.
The text was updated successfully, but these errors were encountered: