-
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
From v2.1.0 wp.blocks.getBlockTypes() returns an empty Array #4848
Comments
There's no different way, you're probably calling this function too soon. The core block registration has been slightly delayed to allow plugin authors to register registration hooks. These delays and hooks are still being optimized, so this might change slightly in the future. |
Hello youknowriad, thank you for your fast reply! I have enqueued the js so:
So the script is included after the the following file: /gutenberg/blocks/build/index.js Do you have a solution for this problem? Thanks! |
The blocks are now registered in the |
Hi, unfortunately adding the js with dependency on Instead if I add my script in the footer adding And the js error is:
So I explain better what I want to do.. so someone may bring me on the right way. I simply have to disable a default block in the editor.. So for example something like this: In the previous version (2.0.0) this function worked like a charm. With the acutal version how can I disable a default block in Gutenberg Editor? Thanks in advance! |
UPDATEI discovered the cause of issue. Here my setup
In the script i added the window.onload function to wait all is loaded..
This setup works but appears an error in react when I unregister a block that is already added in the post. For example if I have a post with an image block active and I unregister the This is a problem because if a user have many posts built with Gutenberg and my plugin disable an important block, the user will always see the react error -> https://www.awesomescreenshot.com/image/3148981/e4b4acd328b6b0cff62bcf61ff994690 Is there a workaround for this issue? Thanks |
This is a similar issue as described by @sheepzilla in #4699. We started exploring different ways of registering blocks to come up with a much reliable solution. You can see what has been proposed already in #2751. Code explorations are done in #4841. So the idea would be to have full control over which blocks are going to be registered on PHP side. |
I'm experiencing the same issue. . .not able to whitelist blocks. Discussed in Slack here (https://wordpress.slack.com/archives/C02QB2JS7/p1523898765000445), but copying details to this issue for more visibility: I'm enqueueing my script like so:
Then in my JS, I console log the registered blocks like so:
This outputs an empty array. If I do it from within a setTimeout, I get an array of 61 blocks logged:
Then, if I set my
Looks good, right? I unregistered all but 2 blocks, and no my Now my console is littered with more errors and the editor becomes useless. Attached is a GIF recording showing the experience with the above code in place. From what I can tell, it appears to me that once a block is registered the DOM (or state tree?) gets built out with the knowledge of said registered blocks. Then, if you come along and unregister block(s), the DOM (or stat tree?) still assumes those block(s) exist and the editor at large no longer knows how to behave with those blocks no longer in existence. |
i am also facing same issue when i try to add foreach of post below is the code snipet
|
@chiraggmodi - I think you are experiencing another issue with race conditions for |
@gziolo i am still not able to solve this isuse. Can you share a javascript snippet? |
@chiraggmodi, I'm afraid, there is nothing to share. You can try with the branch opened together with #6303 or apply this diff locally: https://patch-diff.githubusercontent.com/raw/WordPress/gutenberg/pull/6303.diff. |
@gziolo i have change according to above PR, but same issue no changes. |
fwiw, this is kind of working for me right now. I still get console errors, but it does appear that my blocks are properly whitelisted.
Console errorThis is the error I see with the code above added. But it does seem that the blocks I have whitelisted are indeed whitelisted and no other blocks can be inserted, not even from the |
ugh. . .the setTimeout seems to also only partially solve the issue. Sometimes my blocks are whitelisted and sometimes they are not. Seems like this is a case of my code not being executed at the proper time. . .is there a more proper way to do what I'm trying here, like an action/filter that I can hook the whitelist to? See the GIF below showing that the whitelist is sometimes applied, but on refresh it's sometimes not applied too. First 2 page loads show just the whitelisted blocks. 3rd one shows all 60+ blocks. |
Seems like there's about a half dozen layers of callbacks nowadays which occur before blocks are actually registered. This should work if your script also includes window._wpLoadGutenbergEditor.then( function() {
console.log( wp.blocks.getBlockTypes() );
} ); This is a private function which probably shouldn't be relied upon. Personally, I think the blocks should be registered immediately, as they were in earlier versions of hte plugin. The editor initialization should only be marking specific blocks as active. |
That's an interesting idea. Now, I think a bit more about it. We could also apply all filters at the time those blocks would be marked as active. I think the only reason why it works as it works, it existed to give a chance plugins to modify core blocks properly. It seems like we can achieve the same by doing iteration over all registered blocks and applying filters to them just before the editor gets initialized. |
The PR exploring the blocks in store #6679 would allow us to have fine-tuning around parsing blocks only when their corresponding block get registered. So I was thinking a workflow like this is possible: 1- At Editor initialization, parse the blocks using the grammar only which will give us a list of block names and attributes. 2- A component is rendered for each one of these raw blocks. This component would be reactive to block registration: If the block type corresponding to the block name is registered, complete the parsing of the block (attributes) and render the block like we do currently. (making the block active) 3- If a registered block with the given name is not registered, mark the block as inactive which will show a prompt allowing you to edit it with classic block or html block unless some milliseconds after first rendering the block is registered which will trigger a rerender using the option 2. (Maybe we can have a delay or an event triggering the prompt to use classic block) -- |
@youknowriad @gziolo @aduth Is this issue still relevant or can it be closed? |
As far as I know, the original issue still holds true, with the needed workaround being a "solution". I think a better approach here would be to ensure the blocks are registered immediately, and change the parsing behavior to be more reactive, where an initial pass at content reveals only its raw data, and if/as blocks become registered, their renderings in the editor update accordingly. This would avoid issues we have today where all blocks need to be registered before the editor performs its parse, and would allow plugin authors to reference blocks at any time (after their direct registration from If agreeable, we should consider closing this issue in favor of a new one which is more directed at the actionable task. |
Just noting that I started refactoring which would allow to achieve what @aduth described in his last comment. See #10554. Let's discuss there would be the ideal flow which would satisfy all possible use cases we can imagine when handling registered blocks. I'm closing this one as won't fix as we don't want to invest more time into this issue as it is a consequence of the limitation of the blocks registry existing as of today. |
Probably worth removing or amending the documentation on blacklisting or whitelisting blocks https://wordpress.org/gutenberg/handbook/extensibility/extending-blocks/#removing-blocks until this is resolved. I just spent hours trying to work out what I was doing wrong. |
Issue Overview
Gutenberg Version: 2.1.0
With this version the function wp.blocks.getBlockTypes() returns an empty Array.
Steps to Reproduce (for bugs)
console.log(wp.blocks.getBlockTypes());
Expected Behavior
The returned Array should be so:
Note: tested on v2.0.0
Current Behavior
The Array is empty instead:
Array[]
Note: tested on v2.1.0
Possible Solution
Probably there is a different way to call wp.blocks.getBlockTypes()
Todos
The text was updated successfully, but these errors were encountered: