Skip to content

Commit

Permalink
IBX-947: Modified deleteVersion API method to remove content if neces…
Browse files Browse the repository at this point in the history
…sary

For more details see #3118 or https://issues.ibexa.co/browse/IBX-947
  • Loading branch information
barw4 committed Sep 9, 2021
1 parent 7abf760 commit 0b694bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion eZ/Publish/API/Repository/Tests/ContentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3045,7 +3045,7 @@ public function testDeleteVersionWorksIfOnlyVersionIsDraft()

// This call will fail with a "NotFound", because we allow to delete content if remaining version is draft.
// Can normally only happen if there where always only a draft to begin with, simplifies UI edit API usage.
$this->contentService->loadContent($draft->id);
$this->contentService->loadContentInfo($draft->id);
}

/**
Expand Down
20 changes: 14 additions & 6 deletions eZ/Publish/Core/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,8 @@ protected function getUnixTimestamp()
*/
public function deleteVersion(APIVersionInfo $versionInfo)
{
$contentHandler = $this->persistenceHandler->contentHandler();

if ($versionInfo->isPublished()) {
throw new BadStateException(
'$versionInfo',
Expand All @@ -1881,13 +1883,14 @@ public function deleteVersion(APIVersionInfo $versionInfo)
);
}

$versionList = $this->persistenceHandler->contentHandler()->listVersions(
$versionList = $contentHandler->listVersions(
$versionInfo->contentInfo->id,
null,
2
);
$versionsCount = count($versionList);

if (count($versionList) === 1 && !$versionInfo->isDraft()) {
if ($versionsCount === 1 && !$versionInfo->isDraft()) {
throw new BadStateException(
'$versionInfo',
'Version is the last version of the Content and can not be removed'
Expand All @@ -1896,10 +1899,15 @@ public function deleteVersion(APIVersionInfo $versionInfo)

$this->repository->beginTransaction();
try {
$this->persistenceHandler->contentHandler()->deleteVersion(
$versionInfo->getContentInfo()->id,
$versionInfo->versionNo
);
if ($versionsCount === 1) {
$contentHandler->deleteContent($versionInfo->contentInfo->id);
} else {
$contentHandler->deleteVersion(
$versionInfo->getContentInfo()->id,
$versionInfo->versionNo
);
}

$this->repository->commit();
} catch (Exception $e) {
$this->repository->rollback();
Expand Down

0 comments on commit 0b694bd

Please sign in to comment.