Skip to content

Commit

Permalink
Fix Neo block structure data being lost when soft-deleting entry
Browse files Browse the repository at this point in the history
  • Loading branch information
ttempleton committed Aug 31, 2023
1 parent 729b75b commit 749e6c1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

### Fixed
- Fixed a bug where Neo block structure data would be lost when soft-deleting an entry

## 3.8.6 - 2023-08-10

### Fixed
Expand Down
19 changes: 17 additions & 2 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use benf\neo\gql\types\generators\BlockType as NeoBlockTypeGenerator;
use benf\neo\gql\types\input\Block as NeoBlockInputType;
use benf\neo\jobs\DeleteBlock;
use benf\neo\jobs\SaveBlockStructures;
use benf\neo\models\BlockStructure;
use benf\neo\models\BlockType;
use benf\neo\models\BlockTypeGroup;
Expand Down Expand Up @@ -1166,8 +1167,22 @@ public function beforeElementDelete(ElementInterface $element): bool
if (!$element->hardDelete) {
foreach ($blockStructures as $blockStructure) {
$key = $blockStructure->siteId ?? 0;
Neo::$plugin->blocks->saveStructure($blockStructure);
Neo::$plugin->blocks->buildStructure($blocksBySite[$key], $blockStructure);
$siteId = $key ?: 1;
Queue::push(new SaveBlockStructures([
'fieldId' => $this->id,
'ownerId' => $element->id,
'siteId' => $siteId,
'otherSupportedSiteIds' => [],
'blocks' => array_map(
fn($block) => [
'id' => $block->id,
'level' => $block->level,
'lft' => $block->lft,
'rgt' => $block->rgt,
],
$blocksBySite[$key]
),
]));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/jobs/SaveBlockStructures.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function execute($queue): void
$this->setProgress($queue, 0.3);

foreach ($this->blocks as $b) {
$neoBlock = Neo::$plugin->blocks->getBlockById($b['id'], $this->siteId);
$neoBlock = Neo::$plugin->blocks->getBlockById($b['id'], $this->siteId, ['trashed' => null]);

if ($neoBlock) {
$neoBlock->lft = (int)$b['lft'];
Expand Down
5 changes: 3 additions & 2 deletions src/services/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ class Blocks extends Component
*
* @param int $blockId The Neo block ID to look for.
* @param int|null $siteId The site the Neo block should belong to.
* @param array $criteria
* @return Block|null The Neo block found, if any.
*/
public function getBlockById(int $blockId, ?int $siteId = null): ?Block
public function getBlockById(int $blockId, ?int $siteId = null, array $criteria = []): ?Block
{
return Craft::$app->getElements()->getElementById($blockId, Block::class, $siteId);
return Craft::$app->getElements()->getElementById($blockId, Block::class, $siteId, $criteria);
}

/**
Expand Down

0 comments on commit 749e6c1

Please sign in to comment.