Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix deleted objects are not handled at all #67

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading