Skip to content

Template Part block: Guard against get_block_file_template returning null - #69309

Merged
ockham merged 1 commit into
trunkfrom
fix/template-part-block
Feb 25, 2025
Merged

ockham merged 1 commit into
trunkfrom
fix/template-part-block

Conversation

@ockham

@ockham ockham commented Feb 25, 2025 •

Copy link
Copy Markdown
Contributor

What?

Add a guard to ensure $block_template->content exists before assigning its value to a variable.

Why?

The Template Part PHP code uses get_block_file_template() to fetch information about the given template part from the corresponding theme's block template file. However, that function can return null, and the code currently guard against that when attempting to get the returned $block_template object's ->content field.

How?

See above.

By keeping $content set to its initial null, a later check in the Template Part block code will issue a more user-friendly warning.

Testing Instructions

  • On a site that uses a block theme (e.g. TT5), open the Site Editor, and switch to the code editor.
  • Insert the block markup for a Post Template block that references a template part that doesn't exist (see below for an example).
  • Save the template and view the frontend. The error message should match the "After" screenshot further below.
<!-- wp:template-part {"slug":"post-meta"} /-->

Screenshots or screencast

Before After
image image

@ockham ockham added [Type] Bug An existing feature does not function as intended [Block] Template Part Affects the Template Parts Block labels Feb 25, 2025
@ockham ockham self-assigned this Feb 25, 2025

@paulopmt1 paulopmt1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM.

@ockham

ockham commented Feb 25, 2025 •

Copy link
Copy Markdown
Contributor Author

The "new" error message matches what the user sees in the editor:

image

@ockham
ockham force-pushed the fix/template-part-block branch from 35821a7 to 1e47524 Compare February 25, 2025 16:02
@ockham
ockham marked this pull request as ready for review February 25, 2025 16:04
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ockham <bernhard-reiter@git.wordpress.org>
Co-authored-by: paulopmt1 <paulopmt1@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ockham

ockham commented Feb 25, 2025

Copy link
Copy Markdown
Contributor Author

Flagging this for inclusion in WP 6.8 @Mamaduka @fabiankaegy 🙂

@ockham
ockham merged commit ac34493 into trunk Feb 25, 2025
@ockham
ockham deleted the fix/template-part-block branch February 25, 2025 19:59
@github-actions github-actions Bot added this to the Gutenberg 20.4 milestone Feb 25, 2025
ockham added a commit that referenced this pull request Feb 25, 2025
…g `null` (#69309)

Co-authored-by: ockham <bernhard-reiter@git.wordpress.org>
Co-authored-by: paulopmt1 <paulopmt1@git.wordpress.org>
Co-authored-by: josephscott <josephscott@git.wordpress.org>
@ockham

ockham commented Feb 25, 2025

Copy link
Copy Markdown
Contributor Author

I've cherry-picked ac34493 to the release/20.0 branch.

@Mamaduka

Copy link
Copy Markdown
Member

Thanks, @ockham!

chriszarate pushed a commit to chriszarate/gutenberg that referenced this pull request Jul 1, 2025
…g `null` (WordPress#69309)

Co-authored-by: ockham <bernhard-reiter@git.wordpress.org>
Co-authored-by: paulopmt1 <paulopmt1@git.wordpress.org>
Co-authored-by: josephscott <josephscott@git.wordpress.org>
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 [Type] Bug An existing feature does not function as intended

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

3 participants