Skip to content

Conversation

@ravi-aman
Copy link

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

)

This commit fixes a critical bug where content with plone.translatable
behavior but without plone.dublincore behavior causes runtime errors
when accessing content.language.token.

Changes:
- Add getLanguageToken helper function to safely extract language tokens
- Fix AlternateHrefLangs component to use helper function
- Fix ManageTranslations component to use helper function (7 occurrences)
- Fix Edit component to use helper function (3 occurrences)
- Add comprehensive unit tests for getLanguageToken (8 test cases)

The helper function handles three cases:
1. language is undefined/null → returns null
2. language is a string → returns the string
3. language is an object with token → returns token property

This ensures backwards compatibility while fixing the crash for content
types that don't include plone.dublincore behavior.

Fixes plone#7309
*/
export function getLanguageToken(language) {
if (!language) return null;
if (typeof language === 'string') return language;
Copy link
Member

Choose a reason for hiding this comment

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

The language attribute in Plone is of type schema.Choice. Therefore, it will always be an object. I suggest a simpler fix, with:

content?.language.token

@erral
Copy link
Member

erral commented Nov 26, 2025

Backport also to 18.x.x please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Content without categorization behavior causes errors in view on multilingual sites

3 participants