Skip to content

Fix Category List Block: Add optional chaining to taxonomy name#69703

Merged
Mamaduka merged 1 commit intoWordPress:trunkfrom
Mayank-Tripathi32:try/fix-69697-optional-chaining
Mar 26, 2025
Merged

Fix Category List Block: Add optional chaining to taxonomy name#69703
Mamaduka merged 1 commit intoWordPress:trunkfrom
Mayank-Tripathi32:try/fix-69697-optional-chaining

Conversation

@Mayank-Tripathi32
Copy link
Contributor

Fixes #69697

What?

This PR adds optional chaining (?.) for the name and labels.singular_name properties in the taxonomy object to prevent potential undefined errors.

Why?

Without optional chaining, the block may throw an error when the taxonomy data is unavailable or not properly set. This issue is noticeable when using the Category List block inside a pattern, particularly when enabling "Show as dropdown" and "Show empty values" options.

How?

The implementation adds optional chaining in relevant areas where the taxonomy object or its properties are accessed:

  • Placeholder text for the dropdown (placeholder={ taxonomy?.name })
  • The VisuallyHidden label (label ? label : taxonomy?.name)
  • Dropdown option text (sprintf( __( 'Select %s' ), taxonomy?.labels?.singular_name ))

These changes ensure that the block does not break when the taxonomy object is missing or incomplete.

Testing Instructions

  • Add a new pattern.
  • Insert a Category List block into the pattern.
  • Edit the label and enable Show as dropdown + Show empty values from the inspector panel.
  • Add this pattern to the Site Editor and save the changes.
  • Reload the editor and observe the Category List block.
  • Previously, the block would sometimes show an error when the taxonomy data was unavailable. Now, it should function correctly without breaking.

ScreenCast

If it does not load then it would show (undefined) - Rarely
image

@github-actions
Copy link

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: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: hagege <hage@git.wordpress.org>

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

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Block] Categories Affects the Categories Block labels Mar 26, 2025
Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

Thank you, @Mayank-Tripathi32!

@Mamaduka Mamaduka merged commit b28716a into WordPress:trunk Mar 26, 2025
65 of 68 checks passed
@github-actions github-actions bot added this to the Gutenberg 20.6 milestone Mar 26, 2025
chriszarate pushed a commit to chriszarate/gutenberg that referenced this pull request Jul 1, 2025
…errors (WordPress#69703)

Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: hagege <hage@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Categories Affects the Categories Block [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Category block in navigation: This block encountered an error

3 participants