From ddf46f03f81879047119df9a6f15fead67d403f3 Mon Sep 17 00:00:00 2001 From: Thomas Templeton Date: Fri, 22 Jul 2022 22:37:24 +1000 Subject: [PATCH] Fix error when not autosaving drafts, when validation fails and new block nested in new block --- CHANGELOG.md | 1 + src/Field.php | 2 ++ src/elements/db/BlockQuery.php | 6 +++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f7d180a..c9504e6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fixed a JavaScript error that occurred when pasting a block type - Fixed an incorrect German translation of 'Min Blocks' (thanks @alumpe) - Fixed a bug where the child blocks UI element wasn't appearing on the field layout designer sidebar on existing block types +- Fixed a PHP error that occurred when saving an element that doesn't autosave drafts, if a validation error occurred and there was a new block nested in another new block ## 3.1.8 - 2022-07-15 diff --git a/src/Field.php b/src/Field.php index caec9f8e..ac7afe04 100644 --- a/src/Field.php +++ b/src/Field.php @@ -1107,6 +1107,7 @@ private function _createBlocksFromSerializedData(array $value, ElementInterface /** @var Block[] $blocks */ $blocks = []; $prevBlock = null; + $sortOrderCounter = 1; foreach ($newSortOrder as $blockId) { if (isset($newBlockData[$blockId])) { @@ -1171,6 +1172,7 @@ private function _createBlocksFromSerializedData(array $value, ElementInterface $block->setOwner($element); $block->oldLevel = $block->level; $block->level = $blockLevel; + $block->sortOrder = $sortOrderCounter++; if (isset($blockData['collapsed'])) { $block->setCollapsed((bool)$blockData['collapsed']); diff --git a/src/elements/db/BlockQuery.php b/src/elements/db/BlockQuery.php index 25dd2754..5684611d 100644 --- a/src/elements/db/BlockQuery.php +++ b/src/elements/db/BlockQuery.php @@ -576,7 +576,11 @@ private function _getFilteredResult(): array if (method_exists($this, $method)) { $currentFiltered = $this->$method($this->_allElements, $value); - $result = array_values(array_uintersect($result, $currentFiltered, fn($a, $b) => $a->lft <=> $b->lft)); + $result = array_values(array_uintersect( + $result, + $currentFiltered, + fn($a, $b) => $a->lft ? $a->lft <=> $b->lft : $a->sortOrder <=> $b->sortOrder + )); } }