Fix: Handle undefined language field in multilingual content (#7309)
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.translatable behavior) to content but don't include the full Dublin Core behavior (which has the Language field), Volto crashes with:
Cannot read properties of undefined (reading 'token')
This happens because the code assumes content.language is always an object with a .token property, but it can actually be:
undefined(no language set)- A plain string like
"en" - An object with
.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:
- AlternateHrefLangs - The one throwing the original error
- ManageTranslations - 7 places where it accessed
content.language.token - Edit component - 3 places in the Helmet lang attribute and menu items
Testing
I added 8 unit tests covering all the edge cases:
- undefined/null values
- string values
- object with token
- object without token
- other invalid types
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 entry
Closes #7309
Backport also to 18.x.x please