AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
823 stars 168 forks source link

get_block_wrapper_attributes() doesn't function properly when default attributes are set in block.json #883

Closed dodo-sofranko closed 7 months ago

dodo-sofranko commented 7 months ago

Describe the bug When I set attributes in block.json, then these attr are not appear via get_block_wrapper_attributes() on frontend

To Reproduce Steps to reproduce the behavior:

  1. For example I have own block: FAQ, In block.json I will add to set some default background such code: (there is bug also with paddings ...) "attributes": { "gradient": { "type": "string", "default": "vivid-cyan-blue-to-vivid-purple" }},

  2. In my rendered php I have <section <?= !$is_preview ? get_block_wrapper_attributes() : ''; ?>>

  3. Then I will add my block in gutenberg editor. (default background was set) and I will save

  4. Check frontend, and class has-vivid-cyan-blue-to-vivid-purple-gradient-background is missing there

  5. I will change to another background or gradient, and on frontend is showing correct

  6. then I will change gradient back to vivid-cyan-blue-to-vivid-purple and class is missing

Version Information:

Additional context Thank Dodo

lgladdy commented 7 months ago

Hey there,

Thanks for your feedback, however I believe this is expected behaviour, get_block_wrapper_attributes isn't designed to handle defaults. Usually, these would be handled automatically by the save method on the frontend to be written to the DOM saved inside the block comment, but as ACF doesn't use this, you won't get that support.

Instead, you'll need to manually handle your block defaults - either by making it default or by parsing it out from the block object, which you can read from the $wp_block attribute in your template.

If you'd like us to build a custom ACF Blocks function to do this automatically, please open a feature request on https://advancedcustomfields.com/feedback so our product team can look into it.

Thanks again! Liam

dodo-sofranko commented 7 months ago

we misunderstood,

I don't want to get the default value of attribute via get_block_wrapper_attributes().

But get_block_wrapper_attributes() dont work properly if default value of atribute is set in block.json

on frontend there is missing classes about gradient, and gradient is already set in backend

lgladdy commented 7 months ago

Hey @dodo-sofranko,

I think I did understand correctly, but that's not how get_block_wrapper_attributes works. It's not an ACF function, it's a WordPress function that's designed to take the parameters set in the stored block comment, not have any awareness of it's block.json.

You currently need to manually handle the defaults yourself in your block or css as well when using ACF Blocks.