volto icon indicating copy to clipboard operation
volto copied to clipboard

Fix: Handle undefined language field in multilingual content (#7309)

Open ravi-aman opened this issue 1 month ago • 1 comments

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 .token like { 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 - Added getLanguageToken() helper
  • src/helpers/Content/Content.test.js - Added test coverage
  • src/components/theme/AlternateHrefLangs/AlternateHrefLangs.jsx - Use helper
  • src/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

ravi-aman avatar Nov 19 '25 03:11 ravi-aman

Backport also to 18.x.x please

erral avatar Nov 26 '25 07:11 erral