Skip to content
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

Merged

Conversation

edanzer
Copy link
Contributor

@edanzer edanzer commented Nov 9, 2022

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.

  1. Ensure you have a block theme installed.
  2. Go to the site editor.
  3. Add 3 template part blocks to the page. For each, click 'Start New' to create a new template part, and give each template part a memorable name.
  4. Once all three template parts are added to the page, save the page and reload the screen/site editor.
  5. Click on one of the the template part blocks and confirm the correct block name shows on the block side bar (without this fix, you will frequently get the wrong block name).

Screenshots or screencast

template-part-block-variations.mp4

@edanzer edanzer requested a review from ajitbohra as a code owner November 9, 2022 22:16
@codesandbox
Copy link

codesandbox bot commented Nov 9, 2022

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 9, 2022
@github-actions
Copy link

github-actions bot commented Nov 9, 2022

👋 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.

@ramonjd
Copy link
Member

ramonjd commented Nov 9, 2022

This is working a treat for me. Thanks!

Before
2022-11-10 09 32 07

After

2022-11-10 09 34 01

@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended [Block] Template Part Affects the Template Parts Block labels Nov 9, 2022
Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@ramonjd ramonjd enabled auto-merge (squash) November 9, 2022 22:49
@@ -40,6 +40,10 @@ export function enhanceTemplatePartVariations( settings, name ) {
'wp_template_part',
`${ theme }//${ slug }`
);

if ( entity?.slug ) {
Copy link
Contributor

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?

Copy link
Member

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

@ramonjd ramonjd merged commit f8c3f1f into WordPress:trunk Nov 9, 2022
@github-actions
Copy link

github-actions bot commented Nov 9, 2022

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Template Part Affects the Template Parts Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants