-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block API: Ensure filters are properly applied to blocks regardless when they are registered #9757
Comments
Does your script depend on |
Hooks are not delayed, so in theory (unless I'm missing something), it should work if your script is loaded after the |
@youknowriad Yes the script depends on I'm not saying hooks are delayed, I'm saying when the script is this: wp.blocks.registerBlockType('custom/block', {...});
wp.hooks.addFilter('blocks.registerBlockType', 'custom/filter', function() {...}); then the filter is not applied when In this case, the filter does get applied: wp.domReady(function() {
wp.blocks.registerBlockType('custom/block', {...});
});
wp.hooks.addFilter('blocks.registerBlockType', 'custom/filter', function() {...}); So I'm thinking maybe it should be the default behavior of |
I'm having problems with this type of behaviour too, in #14050 i can use the filters, but when other plugins are active that change the enqueuing behaviour, it can affect when plugins get loaded in relation to wp-blocks, and so my code doesn't run in time for the filters it relies on. I opened a jetpack issue at Automattic/jetpack#11464 (because it was Jetpack activating that showed up the behaviour, and that issue has reproduction instructions) but it seems like something we should fix in Gutenberg |
There was some discussion of this issue in the #core-js meeting, April 16th, 2019 (link requires registration): https://wordpress.slack.com/archives/C5UNMSU4R/p1555424581072000 Summary: https://make.wordpress.org/core/2019/04/17/javascript-chat-summary-april-16th-2019/ |
@aduth I'm having trouble with regards to this issue too. Custom attributes are being applied only on the core blocks. Third party plugins were not affected by the filters. Thanks! |
would it be possible to listen for plugins adding hooks on |
@adamsilverstein to your point. I think it is technically possible to achieve. I tried to investigate that in #10554. We should revisit it again when Block Registration API RFC is finalized. It shouldn't be that difficult to change the flow of blocks registration if we do for example this:
|
Hi Everyone, I've finally found a workaround and seems to be working perfectly. Use I hope this helps! Cheers! |
I linked #6092 to this issue. There are some similar points made:
With the proposed solution being:
More details in the issue, I just posted this to bring the two together. |
@nerrad Thanks for cross-linking. I think the additional action suggestion by @elliotcondon there makes sense. Its essentially a timing based hook that fires at exactly the right time developers can count on and feels like a direct solution to the issue at hand, although I suspect others here will not feel as comfortable with this type of approach. I also do still see a use case for exploring the more dynamic re-registering of blocks when filters are added (or removed) which could enable dynamically adding new block types into a running Gutenberg session for example, or enabling an extension/plugin that uses a registration filter. |
wp.blocks.registerBlockType
be delayed until wp.domReady
?
I am getting this issue as well. I am trying to add attributes to Gutenberg blocks using block filters. It works fine for the core blocks, but not for any 3rd party block. Here is my code.
This filter only works for the core blocks. Any workaround to this? TIA |
We discussed the same issue today during the weekly Core JS chat. It's a very difficult issue to solve that's on our radar: https://wordpress.slack.com/archives/C5UNMSU4R/p1592922624105100 |
That's great to hear that it's on the radar. In the meantime, is there a workaround for this? TIA |
It's tough, usually what happens is that blocks have only a few dependencies so it registers earlier and they can't use the filters that are applied later. One thing that could help is to use as little dependencies as possible for your filters, so probably extract the logic that modifies blocks to their own file. I feel really bad giving this type of answer, but we really need to rethink how blocks are registered on the client. I'm talking mostly about internal implementation. It's doable but it needs some deep understanding of all use cases when the current approach fails. |
I see. What if that 3rd party block is also our own plugin? And we need to modify it from another plugin ( I know this may sound silly, but I do have a use-case ). Any suggestions or pointers to head me in the right direction, please? I appreciate your responses. TIA |
@munirkamal Does wrapping your block registration in wp.domReady not work for you? Example here. |
Oh, I haven't tried that yet. I will try and get back here. Thanks for the reference, I did not notice it earlier. 🙂 |
You can try |
Awesome, thank you for the suggestion. I will try it out and get back with the result here. 🙌 |
Just to update you, unable to make it work that way (as you suggested). 🙁 Looking forward for a fix for this. |
…in. This means when the WordPress/gutenberg#9757 gutenberg bug (feature?!) is fixed, we can perhaps use a standard action in our core plugin, and our add-ons will fall in place automatically.
…in. This means when the WordPress/gutenberg#9757 gutenberg bug (feature?!) is fixed, we can perhaps use a standard action in our core plugin, and our add-ons will fall in place automatically.
As a current workaround, I had to leave |
After hours of debugging I have landed here. Actually the workaround from #9757 (comment) worked in my case. Still it would be nice to have a proper solution and especially have this mentioned somewhere in the documentation of the hook/filter to save people some time. |
I agree that it requires more attention. It will become even more pressing once we have several screens using the block editor. Setting a script dependency to |
I'm exploring whether we can re-apply filters (when necessary) to the registered blocks just before the block editor starts the parsing step in #34299. Ideally, we should be able to apply those filters at any time and all block definitions should magically update, but there are some architectural shortcomings that we would have to address first:
|
Fixes this issue: WordPress/gutenberg#9757
Is your feature request related to a problem? Please describe.
I'm utilizing the
blocks.registerBlockType
filter to add a custom attribute to all blocks, much like the Custom CSS Class feature does it here.I noticed the core blocks received the custom attribute, but my custom blocks didn't.
The reason being that the plugin with my custom blocks is loaded earlier and calls
wp.blocks.registerBlockType()
before the plugin with the filter is loaded. Both plugins' scripts are hooked toenqueue_block_editor_assets
action so I believe it comes down to alphabetical plugin naming or similar.I looked at how Gutenberg registers it's core blocks and found that they are registered inside a
wp.domReady()
call.It never occurred to me to do this because in the docs the
registerBlockType()
is used as-is. But I wrapped mywp.blocks.registerBlockType()
inwp.domReady()
and sure enough, the attributes are now successfully applied.But then I tried a third-party plugin that also registers blocks and the same issue arose.
It just calls
registerBlockType()
and there's nothing(?) I can do to guarantee my plugin with the filter is loaded before that, since they both depend on the samewp-*
script dependencies.So my question is, could the
registerBlockType()
function pool all the calls and set them up to be called after the DOM is ready, so that all scripts have a chance to add their filters? (And after DOM is ready it should register the block immediately.) Or should plugin developers be instructed to not register their blocks before DOM is ready to guarantee compatibility with any plugins that may wish to filter the registration process? Or doblocks.registerBlockType
filter users simply need to take extra measures to ensure their scrips are loaded as early as possible?The text was updated successfully, but these errors were encountered: