-
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
Add ability to access block context from inside render_block
filter
#63626
Comments
@simom I believe the issue might be that you are not defining the In order for the server to be aware and therefore properly pass the context to the |
@fabiankaegy thanks for your quick reply!
I'm not sure if I must define also the |
I have not actually tried to implement this code, but it looks like |
Thanks for your feedback @ndiego and yes, you are right, I pasted the wrong code since I was experimenting with different things. By the way, even with the correct condition, it is not working |
Thanks for confirming. I'll need to put a complete example together to really test this. I'll follow up once I investigate this a bit more. |
render_block
filter
Reopening with a new title. After doing some more research it appears the issue is not that you cannot register new attributes / set the |
Hmm, I think you can. I was just testing with the Social Icons block. If you add a Social Icons block with a few icons to a page, then add the following, the
|
I performed a test and it returns an empty array when trying to access $instance->context. (I've added $instance as parameter to the function) |
Yeah same here. The context is always just an empty array. Even for blocks that already define other |
I got it to work. The following is a bit messy, but you need to make sure you are setting both
Give it a try, and let me know if you run into any issues. One thing to note is that the |
Oh, I see. The only difference is that it must be called Then, you have to check it inside the Thank you so much, Nick. Really appreciated! Can I ask you why this naming difference? and why on a custom block I can access to the context directly from the |
Okay I'll try and also Digg in more :) thanks @ndiego ❤️ will close the issue for now 👍 |
So it depends on the filter you use. The For the context, the |
|
I modified @ndiego's snippet to attempt to pass some context into blocks inside the post template and the context is not passed down. I think I see why so I created a new issue for that. |
Description
I'm trying to extend the core block "list" and the child block "list-item" by adding a custom attribute. This attribute must also be defined as providesContext in the "list" block so it will be available to the child items.
I'm filtering
blocks.registerBlockType
by adding the attribute and the contexts, and in the editor, it works as expected. On the frontend, I'm filtering the list item render with render_block_core/list-item and I'm expecting to retrieve the context with$block->context['list/icon']
, but the context is not present.As suggested in another similar ticket, I've tried to add the use_context via the
register_block_type_args
filter, but without success. Is there a limitation in the core regarding the assignment of a providesContext?Step-by-step reproduction instructions
JS file
PHP file
Screenshots, screen recording, code snippet
No response
Environment info
Please confirm that you have searched existing issues in the repo.
Please confirm that you have tested with all plugins deactivated except Gutenberg.
The text was updated successfully, but these errors were encountered: