Fix: Handle undefined language field in multilingual content (#7309) #7635
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix language token crash in multilingual content
The Problem
I ran into this bug while working with custom content types. When you add translation support (
plone.translatablebehavior) to content but don't include the full Dublin Core behavior (which has the Language field), Volto crashes with:This happens because the code assumes
content.languageis always an object with a.tokenproperty, but it can actually be:undefined(no language set)"en".tokenlike{ token: "en", title: "English" }What I Changed
Added a helper function
getLanguageToken()that safely handles all three cases. Then updated these components to use it:content.language.tokenTesting
I added 8 unit tests covering all the edge cases:
The fix is backwards compatible - existing code with proper language objects keeps working, and now content without Dublin Core doesn't crash anymore.
Files Changed
src/helpers/Content/Content.js- AddedgetLanguageToken()helpersrc/helpers/Content/Content.test.js- Added test coveragesrc/components/theme/AlternateHrefLangs/AlternateHrefLangs.jsx- Use helpersrc/components/manage/Multilingual/ManageTranslations.jsx- Use helper (7 spots)src/components/manage/Edit/Edit.jsx- Use helper (3 spots)news/7309.bugfix- Changelog entryCloses #7309