-
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
Template Part Block: Update block isActive method #45672
Template Part Block: Update block isActive method #45672
Conversation
Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @edanzer! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
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.
🚀
@@ -40,6 +40,10 @@ export function enhanceTemplatePartVariations( settings, name ) { | |||
'wp_template_part', | |||
`${ theme }//${ slug }` | |||
); | |||
|
|||
if ( entity?.slug ) { |
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.
Should we remove optional chaining if ( entity.slug )
, I think if
is enough, how do you think?
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'm not 100% sure about this, but I think it's pretty safe since getEntityRecord()
might not return an object at first., but null
.
See: https://developer.wordpress.org/block-editor/reference-guides/data/data-core/#getentityrecord
Congratulations on your first merged pull request, @edanzer! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts: https://profiles.wordpress.org/me/profile/edit/ And if you don't have a WordPress.org account, you can create one on this page: https://login.wordpress.org/register Kudos! |
What?
Fixes a bug in the isActive method for the template part block to ensure we match blocks to the correct block variation.
Why?
A recent commit (block-library/src/template-part/index.php) updated the relationship between template parts and block variations so that every template part is now its own block variation.
However, the template part isActive() method was not updated, and frequently returns the wrong block variation.
We match template parts to variations using the block's area attribute. But most new template parts (and their associated block variations) have the same area ('uncategorized'). So if you add three template part blocks to a page, each will generate a block variation with the same area attribute. Later, when we try to match one of those blocks to a block variation, we'll look for any core/template-part block with uncategorized/general as the area. There are three of them, and we simply find the first match, which may or may not be the right one.
I've recorded a short video (link below) showing one of the symptoms of this issue for clarity.
How?
This PR updates the isActive method for the template part block to use the block slug rather than block area. The block slug is more specific. We match a block to a variation based on slug if a slug exists, and if not, we still fall back to the area attribute.
Testing Instructions
This bug likely has a range of small implications. We found it because we're doing some testing in wp-calypso that confirms block variations. The test below is design to confirm one specific symptom of the bug is resolved. In essence, we want to confirm we're showing the correct block name in the block sidebar. See the video above for details if needed.
Screenshots or screencast
template-part-block-variations.mp4