-
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
Server side block styles - add default value support #24300
Server side block styles - add default value support #24300
Conversation
Size Change: +15 B (0%) Total Size: 1.16 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this with the Site Title, and Site Tagline blocks. Added the style attribute and confirmed on refresh that the styles were applied. 👍
One concern -- these default styles applied even though I had manual style changes already to the blocks. I think this was because the blocks were already present and modified before applying these code changes, I could not reproduce when adding new blocks to the site editor.
* @return array Colors CSS classes and inline styles. | ||
*/ | ||
function gutenberg_experimental_build_css_colors( $attributes, $block_attributes, $supports ) { | ||
function gutenberg_experimental_build_css_colors( $attributes, $block_attributes, $supports, $block_type ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is getting really long, and has a lot of conditionals. Not in this PR, but I think it would be good to consider ways to simplify and break out different parts into separate functions. With the number of conditionals it's becoming harder to follow.
Interesting! I am finding that if we have named default values they can overwrite the saved custom values in the editor but not the front-end. Is this what you are experiencing @apeatling ? Also which theme are you testing on? (Im noticing it is visible on some but not others, but with the problematic class names being added in either case). I am finding this happening on master as well, and it seems specific to the server side blocks. Although, it seems standard blocks have some separate issues in the same circumstance. I will investigate more and write up some issues with more details. Edit - From what I am seeing the default styles applied by the changes in this PR are working as expected, but there are some other issues with them being added in the editor that already exist if you test similarly on master #24363. (and also stumbled upon another issue with simple blocks like paragraph/heading - #24387) |
@youknowriad - do you think this approach makes sense for how we should apply default defined values for these hooks? It seems to work well (not counting the already existing in-editor weirdness noted in #24363). If so, tomorrow I can rebase/reorg these based on your recent refactoring of these functions. 😁 |
Thanks for the work here, though, I'm hesitant personally because of a few considerations:
That said, I don't have an alternative solution for that. So whether we accept that default values don't work on the server (like attributes parsing), or we move forward with this 50%solution for support flags. I'd like more opinions on this from other contributors @mcsf @mtias |
Interesting! For non-supported attributes maybe the solution is block developers handling this case in the blocks specific index.php file? For attributes related to our support flags however, it would be really nice if default values were supported and handling them as fallbacks where saved values are applied seems to make sense. |
@mcsf - was that 👍 for:
or did you have other thoughts? I feel it would be good to support default values for the supported styles at the least. My main motivation for this (besides completeness of these style applications) was to see about allowing default block alignment of But if we support this and do not set the default to Hence starting with 🤔 Maybe there are other ways to get around this problem, but applying the |
Indeed, that was a bit opaque of me. :) I was trying to come to a position on this before I could explain my thoughts, but essentially my 👍 was an acknowledgement that this is a thorny issue. I fully understand the end-user benefits of letting Post Content default to Some questions on my mind:
|
@mcsf thank you for the insights! A couple questions:
Does this mean that if a themes markup file for a template includes a template part with attribute
I wondered about this direction as well, it may make more sense! iirc Global Styles would do something similar to apply globally saved values before Moving forward from here So if I am understanding correctly, instead of applying the default values 'ad hoc' as fallbacks as we are in this PR we should create a function to run before If -
I think this does make more sense than the current approach in this PR. |
@mcsf - expanding more on my questions above:
Doesn't the render_block filter only filter the actual content string? Or is there a way to use this to filter and return the actual |
Closing this as it is the wrong approach. Opened #24909 - we can continue discussion over there. |
@Addison-Stavlo: I got back from holidays and am still catching up on things. 😓
Correct.
To your question of whether you're understanding correctly: yes. :) That said, I don't like what past-me suggested, so... mostly disregard it. Overall, there are a bunch of fragilities in how we're implementing Global Styles as far as the back end is concerned. Adding more logic like automatically resolving default attributes on the server (knowing that we can only support explicit (non-sourced) attributes and that we can't perform proper block validation), especially when it's done magically without the block author's intervention or realisation, gets us deeper into this problem. We need to rethink a lot of this, and I don't yet have good ideas to propose.
Yeah, it needs to return a string. Strictly speaking, I suppose that block authors could get clever with reference-passed arguments (Gutenberg source, PHP references), but I hope I never see that. 😅 |
Interesting, I may have some more thoughts later but for now im curious about:
I am a bit more confused here. It seems like we must be able to do this in some cases as attributes like |
We might be getting our wires crossed. I'd like to understand this better. Can you DM me once you're around today? :) |
We definitely were. 😆 Thanks for the chat! For visibility I summarized the takeaways from the convo on the other PR - #24909 (comment) |
Description
Following up on the style support added for server side blocks in #23007 - This PR adds support for default values to be applied. These default values are already picked up by the corresponding hooks in the editor, but in the case of server side blocks are never applied on the front end.
How has this been tested?
There are not currently any server side blocks in Gutenberg that define defaults for these values. In my testing I have added default values (and their corresponding support flags if necessary) to current blocks (such as Post Author). After verifying all default value styles are applied as expected to the front-end, I added tests in
class-block-supported-styles-test.php
to make this easier to see. Each tests verifies the same output as its saved value counterpart, but is set up to have no attributes saved and defaults defined instead.To test this manually for a server side block, you can add default values (and their corresponding support flags) to its
block.json
file under attributes. (see below)For named default values you can add the following attributes (note - backgroundColor and gradient to be added and tested separately since they are both for the background):
For custom default values you can add the following to attributes (same note with background and gradient to be tested separately):
also, be sure the required flags are added under
supports
in block.json:Add the block to a post in the editor, save, and view it on the front-end. You should see the styles visually applied, and their corresponding classes and/or inline styles present on the wrapping element.
Screenshots
Before - A server side block with hook style support with default values defined but not applied:
After - Default values applied as expected:
Types of changes
New feature (non-breaking change which adds functionality)
Checklist: