Skip to content

Commit

Permalink
Fix error when not autosaving drafts, when validation fails and new b…
Browse files Browse the repository at this point in the history
…lock nested in new block
  • Loading branch information
ttempleton committed Jul 22, 2022
1 parent 941ecd8 commit ddf46f0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down Expand Up @@ -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']);
Expand Down
6 changes: 5 additions & 1 deletion src/elements/db/BlockQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}
}

Expand Down

0 comments on commit ddf46f0

Please sign in to comment.