I have a dynamic block that makes an API call to grab the latest posts of a custom post type. This block has Inspector Controls options for selecting which post type and how many posts to show. It is rendered on the front end, of course, with PHP.
The $attributes
array is passed into the PHP rendering function just fine, however, if a block attribute default is used then that value is not included.
For example, the block has defaults of using the custom post type "horse" and the number of posts "10". If these defaults are left as-is and not changed, then the $attributes
array in the callback function is empty.
I can get around this for my specific situation by knowing exactly what the defaults are, but this seems like it might be a bit of an oversight? Or maybe I'm missing something? 🤷♀️
$attributes
array is empty if the defaults are not changed (I used return implode( ' ', $attributes);
Using Gutenberg 2.6.
I would expect that the default values for the attributes would be included in the $attributes
array that is passed to the callback function for server-side rendering.
The default values from the block schema are supposed to be included if default
values are present:
Please show your code.
Sure, thanks for taking a look @westonruter. Here are the block files: https://github.com/raquelmsmith/strided-app/tree/master/blocks/strided-member-content
And here's the callback function: https://github.com/raquelmsmith/strided-app/blob/master/admin/class-strided-app-admin.php#L479-L515
@raquelmsmith thanks. I see the problem is that you're not registering your block via PHP and so the server doesn't know about the default attributes. Try following the model of the Latest Posts block in registering the block: https://github.com/WordPress/gutenberg/blob/4c90150fb3069e6c2785a0df267a4f344ace4a03/blocks/library/latest-posts/index.php#L68-L110
Ah! I didn't see this in any of the examples I was following - most totally ignored having any attributes being passed to the PHP function. Added the attributes to my PHP register block type
and now it's working great. Thanks!
@westonruter are the attribute definitions a 1:1 mapping of the JS ones? (currently porting to dynamic block and wondering why attributes is an empty array when it was working in JS, added attributes as you show now but still have the issue)
They should be a 1:1 mapping. Hard to know what issue you're having without seeing your code. Nevertheless, I'm probably not the best person to ask.
Thanks I removed the selector stuff and just kept type and recreated the
block and it started working. Documentation is very sparse.
On Thu, 21 Nov 2019 at 04:13, Weston Ruter notifications@github.com wrote:
They should be a 1:1 mapping. Hard to know what issue you're having
without seeing your code. Nevertheless, I'm probably not the best person to
ask.—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/WordPress/gutenberg/issues/6187?email_source=notifications&email_token=AAFZT6SWG5G6WTUP4QD6NTLQUWK5FA5CNFSM4E2WJXB2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEUP4PY#issuecomment-556334655,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAFZT6QGCKRRFZ3FEGE6OR3QUWK5FANCNFSM4E2WJXBQ
.
Most helpful comment
@raquelmsmith thanks. I see the problem is that you're not registering your block via PHP and so the server doesn't know about the default attributes. Try following the model of the Latest Posts block in registering the block: https://github.com/WordPress/gutenberg/blob/4c90150fb3069e6c2785a0df267a4f344ace4a03/blocks/library/latest-posts/index.php#L68-L110