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
Support for nested attributes and post meta #5191
Comments
AFAIK post_meta don't allow storing objects, a workaround for now is to use a unique attribute and use JSON encode it or something. I wonder if this issue should be raised in Trac first as Gutenberg can't support these types as meta if Core doesn't support it. |
@youknowriad yeah, you're right. based on your suggestion, I was able to hack my way around this by creating another attribute and storing the nested object as a string. Something like this:
Then I register the post meta like this:
With this, I can get the object doing a
This works, but is not the optimal solution. I would love to create a ticket on Trac about this, but I'm not sure what we need to do and where. Can you give me some directions on what should I put on the ticket, or where we could change to allow this kind of stuff? Sorry if it's too much to ask, I would love to contribute but never did a Trac ticket before. |
I think the issue should be something like: "support associative array type in register_meta". The underlying implementation could rely on |
@youknowriad thanks for the help! I just opened a ticket on Trac for this. Here is the link for reference: https://core.trac.wordpress.org/ticket/43392 |
Added labels to refer to later and closing as there is matching core track ticket. |
Issue Overview
Currently, Gutenberg handles variables on the
attributes
object. It works pretty well if you have simple attributes, like this:Also, you can save this attribute as post meta by doing this:
And then, you declare the existence of this meta like this:
Which also works pretty well. But on some cases, you might end up with a lot of attributes, and if you want to save them as post meta, it's not practical to save each single attribute into it's own post meta. On this case, it makes sense to group attributes into a single one. Something like this:
But there are two problems right now that prevents nested attributes to work:
1:
props.setAttributes
doesn't allow updates on nested objectsCurrently, you can't do something like this:
The only way you can update a nested attribute is by updating the whole attribute. So my current workaround is like this:
Which is not practical, but works right now. I'm basically cloning the original attribute, changing the value, and updating the whole attribute.
2:
register_meta
typeI tried to use
register_meta
to set thenavigationStyles
attribute as thenavigation_styles
post meta, but it didn't worked. I suspect that it is related to thetype
parameter on theregister_meta
function:The
navigationStyles
attribute is an Object on javascript, but we should save its post meta as a multidimensional array. I'm not sure if currently there's a way to settype
asobject
orarray
. So, if we do have something that works with a nested attribute, please let me know! 😃Expected Behavior
Nested attributes should be allowed by default. Also,
props.setAttributes
should allow updating a nested attribute. And the whole attribute (on my example,navigationStyles
) should be saved as a single multidimensional array post meta.Current Behavior
Attributes are only single level. To have a nested attribute it is necessary to hack your way around the way
props.setAttributes
works. Also, it's not clear what you should use for thetype
parameter for theregister_meta
function.Possible Solution
Allowing a nested structure (something like my example above) would be great. Also,
props.setAttributes
could allow updating nested attributes like this:The text was updated successfully, but these errors were encountered: