Skip to content

Commit

Permalink
FEATURE: Prevent deletion of referenced asset collections
Browse files Browse the repository at this point in the history
Resolves: #245
  • Loading branch information
Sebobo committed Oct 29, 2024
1 parent 0712073 commit d09b62a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
31 changes: 31 additions & 0 deletions Classes/GraphQL/Resolver/Type/AssetCollectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Flowpack\Media\Ui\Service\AssetCollectionService;
use Neos\Flow\Annotations as Flow;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Neos\Domain\Model\Site;
use Neos\Neos\Domain\Repository\SiteRepository;
use t3n\GraphQL\ResolverInterface;
use Neos\Media\Domain\Model\AssetCollection;
use Neos\Flow\Persistence\PersistenceManagerInterface;
Expand Down Expand Up @@ -44,6 +46,14 @@ class AssetCollectionResolver implements ResolverInterface
*/
protected $assetCollectionService;

/**
* @Flow\Inject
* @var SiteRepository
*/
protected $siteRepository;

protected array|null $siteDefaultAssetCollections = null;

public function id(AssetCollection $assetCollection): string
{
return $this->persistenceManager->getIdentifierByObject($assetCollection);
Expand All @@ -53,4 +63,25 @@ public function assetCount(AssetCollection $assetCollection): int
{
return $this->assetCollectionService->getAssetCollectionAssetCount($this->id($assetCollection));
}

/**
* Returns true if the asset collection is empty and is not assigned as default collection for a site
*/
public function canDelete(AssetCollection $assetCollection): bool
{
if ($this->siteDefaultAssetCollections === null) {
$this->siteDefaultAssetCollections = [];
/** @var Site $site */
foreach ($this->siteRepository->findAll() as $site) {
$siteAssetCollection = $site->getAssetCollection();
if (!$siteAssetCollection) {
continue;
}
$this->siteDefaultAssetCollections[$this->id($site->getAssetCollection())] = true;
}
}

return !array_key_exists($this->id($assetCollection), $this->siteDefaultAssetCollections)
&& $this->assetCount($assetCollection) === 0;
}
}
9 changes: 6 additions & 3 deletions Classes/GraphQL/Resolver/Type/MutationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,13 @@ public function deleteAssetCollection($_, array $variables): array
throw new Exception('Asset collection not found', 1591972269);
}

if ($this->assetCollectionService->getAssetCollectionAssetCount($id) > 0) {
throw new Exception('Asset collection is not empty', 1730102095);
}

/** @noinspection PhpUndefinedMethodInspection */
foreach ($this->siteRepository->findByAssetCollection($assetCollection) as $site) {
$site->setAssetCollection(null);
$this->siteRepository->update($site);
if ($this->siteRepository->findOneByAssetCollection($assetCollection)) {
throw new Exception('Asset collection is referenced as default collection of a site', 1730101671);
}

$this->assetCollectionRepository->remove($assetCollection);
Expand Down
1 change: 1 addition & 0 deletions Resources/Private/GraphQL/schema.root.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ type AssetCollection {
tags: [Tag!]!
assetCount: Int!
path: AssetCollectionPath
canDelete: Boolean!
}

"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const DeleteButton: React.FC = () => {
style="transparent"
hoverStyle="error"
disabled={
(!selectedAssetCollection || !config.canManageAssetCollections) &&
(!selectedAssetCollection || !config.canManageAssetCollections || !selectedAssetCollection.canDelete) &&
(!selectedTag || !config.canManageTags)
}
title={translate('assetCollectionTree.toolbar.delete', 'Delete')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const ASSET_COLLECTION_FRAGMENT = gql`
}
assetCount
path
canDelete
}
${TAG_FRAGMENT}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ interface AssetCollection extends GraphQlEntity {
tags?: Tag[];
assetCount: number;
path?: string;
canDelete: boolean;
}

0 comments on commit d09b62a

Please sign in to comment.