Skip to content

Fix TypeError in switchActiveLanguage on pages without primary LSB#1490

Open
GasparSukk wants to merge 1 commit into
qtranslate:masterfrom
GasparSukk:fix/switch-active-language-undefined-tabswitches
Open

Fix TypeError in switchActiveLanguage on pages without primary LSB#1490
GasparSukk wants to merge 1 commit into
qtranslate:masterfrom
GasparSukk:fix/switch-active-language-undefined-tabswitches

Conversation

@GasparSukk

Copy link
Copy Markdown

Summary

switchActiveLanguage() throws TypeError: undefined is not an object (evaluating 'a.length') on admin pages that do not render the primary LSB meta-box — ACF options pages are the most common case, but any page that only renders per-field/editor LSBs (no qtranxs-meta-box-lsb) is affected.

Root cause

js/core/hooks/handlers.js line 888 and 900:

const tabSwitches = _tabSwitchElements[_activeLanguage];
for (let i = 0; i < tabSwitches.length; ++i) { ... }

_tabSwitchElements is populated only by createSetOfLSB() (the primary meta-box LSB). On pages that don't use it, _tabSwitchElements[_activeLanguage] is undefined and .length throws. The crash aborts the language switch, so nothing updates.

This is a regression introduced in 3.16.0 via the JS hooks refactor (#1464 / #1469). 3.15.x did not touch _tabSwitchElements inside switchActiveLanguage.

Fix

Guard both reads with || [] so the loop no-ops when there are no primary LSBs to update. Per-field/editor LSBs and the rest of the switchActiveLanguage flow (wp.hooks actions, _onLoadLanguage, etc.) continue normally.

-        const tabSwitches = _tabSwitchElements[_activeLanguage];
+        const tabSwitches = _tabSwitchElements[_activeLanguage] || [];

Applied at both call sites in switchActiveLanguage.

Test plan

  • Reproduce on 3.16.1: open an ACF options page, click any per-field language tab → TypeError in console.
  • With the patch applied: language switches work across all fields on the same page.
  • Regression: post-edit screen (which DOES have the primary LSB meta-box) still toggles the primary LSB active class correctly, because the key is populated normally.

switchActiveLanguage() reads _tabSwitchElements[_activeLanguage] and
iterates its .length at two spots. On admin pages that do not render
the primary LSB meta-box (ACF options pages, for example), the key is
never populated and the access throws:

    TypeError: undefined is not an object (evaluating 'a.length')

Guard both reads with '|| []' so the function no-ops when there are
no primary LSBs to update — per-field/editor LSBs and the rest of the
switchActiveLanguage flow continue normally.

Regression introduced in 3.16.0 via the JS hooks refactor (qtranslate#1464 /
qtranslate#1469); 3.15.x did not touch _tabSwitchElements in this path.
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.

1 participant