From 48c0064e748238a2551df71ae8824d092aa54804 Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Mon, 27 Mar 2023 12:34:18 +0100 Subject: [PATCH 1/3] ensure matrix blocks added on Matrix->inputHtml() are registered as delta changes --- src/fields/Matrix.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/fields/Matrix.php b/src/fields/Matrix.php index f98caa7c3ac..a99471834a1 100644 --- a/src/fields/Matrix.php +++ b/src/fields/Matrix.php @@ -728,6 +728,14 @@ protected function inputHtml(mixed $value, ?ElementInterface $element = null): s // Safe to create the default blocks? if ($createDefaultBlocks) { + // @link https://github.com/craftcms/cms/issues/12973 + // for matrix fields with minBlocks set Craft.MatrixInput.addBlock() is called before new Craft.ElementEditor(), + // so when we get our initialSerializedValue() for the ElementEditor, + // the matrix block is already there which means the field is reported as not changed since the init + // and so not passed to PHP for save + if ($element->getIsUnpublishedDraft()) { + $view->setInitialDeltaValue($this->handle, null); + } $blockTypeJs = Json::encode($blockTypes[0]->handle); for ($i = count($value); $i < $this->minBlocks; $i++) { $js .= "\nmatrixInput.addBlock($blockTypeJs, null, false);"; From edcbfc2b62cbcdc9979c428bc13ec1b12078b8fd Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 29 Mar 2023 14:13:48 -1000 Subject: [PATCH 2/3] Only set the default value if there are any blocks to add --- src/fields/Matrix.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/fields/Matrix.php b/src/fields/Matrix.php index a99471834a1..f13186f365f 100644 --- a/src/fields/Matrix.php +++ b/src/fields/Matrix.php @@ -727,15 +727,14 @@ protected function inputHtml(mixed $value, ?ElementInterface $element = null): s ');'; // Safe to create the default blocks? - if ($createDefaultBlocks) { + if ($createDefaultBlocks && count($value) < $this->minBlocks) { // @link https://github.com/craftcms/cms/issues/12973 // for matrix fields with minBlocks set Craft.MatrixInput.addBlock() is called before new Craft.ElementEditor(), // so when we get our initialSerializedValue() for the ElementEditor, // the matrix block is already there which means the field is reported as not changed since the init // and so not passed to PHP for save - if ($element->getIsUnpublishedDraft()) { - $view->setInitialDeltaValue($this->handle, null); - } + $view->setInitialDeltaValue($this->handle, null); + $blockTypeJs = Json::encode($blockTypes[0]->handle); for ($i = count($value); $i < $this->minBlocks; $i++) { $js .= "\nmatrixInput.addBlock($blockTypeJs, null, false);"; From d04b1b400402792255761d9beb4fefda6b7f1ca1 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 29 Mar 2023 14:15:23 -1000 Subject: [PATCH 3/3] Release note --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc257d31631..64c4a75605b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Fixed a bug where selectize inputs didn’t have a minimum width. ([#12950](https://github.com/craftcms/cms/issues/12950)) - Fixed a bug where the wrong tab would appear to be initially selected after an autosave, if the selected tab had changed during the autosave. ([#12960](https://github.com/craftcms/cms/issues/12960)) - Fixed a bug where it wasn’t possible to add a Dropdown field without a blank option to a global set. ([#12965](https://github.com/craftcms/cms/issues/12965)) +- Fixed a bug where automatically-added Matrix blocks (per the field’s Min Blocks setting) were getting discarded if no changes were made to them. ([#12973](https://github.com/craftcms/cms/issues/12973)) ## 4.4.5 - 2023-03-21