Skip to content

[6.x] Port matrix blocks#19276

Open
brianjhanson wants to merge 7 commits into
6.xfrom
bugfix/cms-19161-matrix-blocks
Open

[6.x] Port matrix blocks#19276
brianjhanson wants to merge 7 commits into
6.xfrom
bugfix/cms-19161-matrix-blocks

Conversation

@brianjhanson

Copy link
Copy Markdown
Contributor

Refactors the MatrixInput and related files into the new code style. There is a new craft-matrix-input web component that will instantiate the MatrixInput class which is a port from the legacy Craft.MatrixInput code but in typescript and without jQuery.

A compatibility shim is provided by the modules/matrix/index file and is imported into the legacy bundle for the twig code to use.

Related issues

Fixes #19161

brianjhanson and others added 7 commits July 20, 2026 09:54
The JS action clients have always sent X-Registered-Asset-Bundles /
X-Registered-Js-Files headers, but nothing server-side read them, so
every XHR-rendered fragment (Matrix add-block, slideouts, etc.) shipped
the entire page asset stack. Re-executing those bundles against a live
page threw ([exposes-loader] Garnish global) and clobbered JS class
identity, leaving XHR-delivered fragments like nested Matrix inputs
without working JS.

RegisteredClientAssets now mirrors Craft 5's mechanism: it parses the
headers, InternalAssetRegistry and HtmlStack::jsFile() skip re-shipping
anything the browser reported (crc32 hashes, same format as the legacy
view's resource hashes), and non-ajax body-end renders emit a standalone
script recording what was registered onto Craft.registeredAssetBundles/
registeredJsFiles for the clients to echo back. A standalone tag, not
part of the shared inline-JS blob, so an unrelated script error can't
prevent the recording.

Verified against the dev site: a Matrix create-entry response drops from
57.7KB with 16 re-shipped bundle scripts to 22.5KB with none, while
keeping the new block's init JS.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Craft.MatrixInput and Craft.MatrixInput.Entry (1,500 lines of jQuery in
the legacy matrix webpack bundle) become resources/js/modules/matrix:
MatrixInput + MatrixEntry controller classes on @craftcms/garnish Base, a
<craft-matrix-input> custom element, WeakMap registries, and an index.ts
shim that keeps the PHP-emitted boot script and flash JS working
(constructor-compatible global, statics, .Entry).

Legacy-runtime seams are typed and centralized in interop.ts:
Garnish.Select, Craft.Tabs, Craft.FormObserver, and Craft.ElementEditor
have no modern ports yet and are reached through the page globals, and
the instances mirror themselves into jQuery data because PHP-emitted
snippets read $(el).data('matrix'/'entry'). Velocity animations become
Web Animations API calls honoring reduced motion.

MatrixAsset no longer ships the legacy bundle; the module is loaded by
both Vite entrypoints. Verified end-to-end on the dev site: real-click
add on the top-level field and on a nested matrix delivered via XHR
(correct namespace chains, booted controllers), collapse/expand with
preview text and localStorage persistence, action-menu state, drag-sort
registration.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Matrix::blockInputHtml() now wraps the input template in the module's
custom element (entry-types / input-name-prefix / settings attributes,
written fully namespaced) instead of emitting a `new Craft.MatrixInput`
boot script. The min-entries auto-add JS becomes an `addDefaultEntries`
setting handled by the MatrixInput class after its afterInit event; the
server-side DeltaRegistry reset stays put.

Nested matrix inputs arriving in XHR-rendered block HTML now boot
themselves on insertion via custom-element upgrade, with no dependency
on the response's executed JS.

Also aligns the module's imports with this branch's @craftcms/ui package
name (the cherry-picks were authored pre-rename).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Clearing every entry posts `fields[handle]=''`, the Yii-era sentinel the
normalize path matches with `$value === ''` to override the query result
to empty. Under Laravel that sentinel never arrives: the global
ConvertEmptyStringsToNull middleware turns it into null, which fell
through to the lazy-load branch — so the field looked unchanged and
NestedElementManager never deleted anything, silently resurrecting the
removed entries on save.

normalizeValueFromRequest(null) now means "all entries removed", which
is unambiguous because delta only applies request values for fields that
were actually modified. Outside a request, null still lazy-loads.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

📚 Storybook previews

@craftcms/uiopen Storybook

No changed components detected in this Storybook.

resources/jsopen Storybook

No changed components detected in this Storybook.

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