Skip to content

Commit

Permalink
Recursively delete nested Matrix entries (and their FormObserver's)
Browse files Browse the repository at this point in the history
Fixes #16540
Resolves #16560
[ci skip]
  • Loading branch information
brandonkelly committed Jan 28, 2025
1 parent 3cda334 commit ed59376
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Fixed a bug where tooltips were displaying behind slideouts. ([#16529](https://github.com/craftcms/cms/issues/16529))
- Fixed a bug where field translation indicators and action menu buttons could be autofocussed when creating a new entry within a Matrix field, or opening an element editor slideout. ([#16528](https://github.com/craftcms/cms/issues/16528))
- Fixed a bug where field values copied from another site weren’t always saving. ([#16537](https://github.com/craftcms/cms/issues/16537))
- Fixed errors that could occur on Ajax requests when deleting an inline-editable Matrix block. ([#16540](https://github.com/craftcms/cms/issues/16540))

## 5.6.1 - 2025-01-22

Expand Down
1 change: 1 addition & 0 deletions src/templates/_components/fieldtypes/Matrix/block.twig
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
{% tag 'div' with {
class: [
'matrixblock',
'js-deletable',
not entry.enabled ? 'disabled-entry',
static ? 'static',
]|filter,
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/matrix/dist/MatrixInput.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/matrix/dist/MatrixInput.js.map

Large diffs are not rendered by default.

41 changes: 40 additions & 1 deletion src/web/assets/matrix/src/MatrixInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@

this.trigger('afterInit');
}, 100);

// If this field is nested within something that's deletable, be ready to handle that
this.$container.closest('.js-deletable').on('delete', (ev) => {
// Ignore delete events that came from nested elements
if (ev.target === ev.currentTarget) {
this.destroy();
}
});
},

canAddMoreEntries: function () {
Expand Down Expand Up @@ -356,6 +364,19 @@
get maxEntries() {
return this.settings.maxEntries;
},

destroy: function () {
this.entrySort?.destroy();
this.entrySelect?.destroy();
delete this.entrySort;
delete this.entrySelect;

this.$entriesContainer.children('.matrixblock').each((i, container) => {
$(container).data('entry')?.destroy();
});

this.base();
},
},
{
defaults: {
Expand Down Expand Up @@ -925,10 +946,12 @@
}
}

this.actionDisclosure.hide();
this.actionDisclosure?.hide();
},

selfDestruct: function () {
this.destroy();

// Remove any inputs from the form data
$('[name]', this.$container).removeAttr('name');

Expand Down Expand Up @@ -1154,5 +1177,21 @@
// re-grab dismissible tips, re-attach listener, hide on re-load
this.matrix.elementEditor?.handleDismissibleTips();
},

destroy: function () {
this.actionDisclosure?.hide();

this.tabManager?.destroy();
this.actionDisclosure?.destroy();
this.formObserver?.destroy();
delete this.tabManager;
delete this.actionDisclosure;
delete this.formObserver;

// alert any nested inputs that we're getting deleted
this.$container.trigger('delete');

this.base();
},
});
})(jQuery);

0 comments on commit ed59376

Please sign in to comment.