Skip to content

Commit

Permalink
Merge pull request #67 from valantic/fix-deleted-objects-are-not-handled
Browse files Browse the repository at this point in the history
fix deleted objects are not handled at all
  • Loading branch information
limenet committed Mar 26, 2024
2 parents dee7bb3 + f5c2007 commit 9a5eee3
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/EventListener/Pimcore/ChangeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,24 @@ public function __construct(

public function handle(AssetEvent|DataObjectEvent|DocumentEvent $event): void
{
if (!$this->shouldHandle($event)) {
$element = $this->prepareHandle($event);

if ($element === null) {
return;
}

$element = $event->getElement();
$this->messageBus->dispatch(new RefreshElement($this->getFreshElement($element)));
}

// If a folder is created in the assets section in Pimcore 11 the type is set to Unknown.
// https://github.com/pimcore/pimcore/issues/16363
if ($element instanceof Asset\Unknown && $element->getType() === 'folder') {
public function handleDeleted(AssetEvent|DataObjectEvent|DocumentEvent $event): void
{
$element = $this->prepareHandle($event);

if ($element === null) {
return;
}

$this->messageBus->dispatch(new RefreshElement($this->getFreshElement($element)));
$this->messageBus->dispatch(new RefreshElement($element));
}

public static function enableListener(): void
Expand All @@ -66,16 +71,33 @@ public static function getSubscribedEvents(): array
return [
AssetEvents::POST_ADD => 'handle',
AssetEvents::POST_UPDATE => 'handle',
AssetEvents::PRE_DELETE => 'handle',
AssetEvents::POST_DELETE => 'handleDeleted',
DataObjectEvents::POST_ADD => 'handle',
DataObjectEvents::POST_UPDATE => 'handle',
DataObjectEvents::PRE_DELETE => 'handle',
DataObjectEvents::POST_DELETE => 'handleDeleted',
DocumentEvents::POST_ADD => 'handle',
DocumentEvents::POST_UPDATE => 'handle',
DocumentEvents::PRE_DELETE => 'handle',
DocumentEvents::POST_DELETE => 'handleDeleted',
];
}

private function prepareHandle(AssetEvent|DataObjectEvent|DocumentEvent $event): Asset|Document|AbstractObject|null
{
if (!$this->shouldHandle($event)) {
return null;
}

$element = $event->getElement();

// If a folder is created in the assets section in Pimcore 11 the type is set to Unknown.
// https://github.com/pimcore/pimcore/issues/16363
if ($element instanceof Asset\Unknown && $element->getType() === 'folder') {
return null;
}

return $element;
}

/**
* The object passed via the event listener may be a draft and not the latest published version.
* This method retrieves the latest published version of that element.
Expand Down Expand Up @@ -118,10 +140,6 @@ private function shouldHandle(AssetEvent|DataObjectEvent|DocumentEvent $event):
return true;
}

if ($event instanceof DocumentEvent && $this->configurationRepository->shouldHandleDocumentAutoSave()) {
return true;
}

return false;
return $event instanceof DocumentEvent && $this->configurationRepository->shouldHandleDocumentAutoSave();
}
}

0 comments on commit 9a5eee3

Please sign in to comment.