Skip to content

Commit

Permalink
Merge pull request #90 from dachcom-digital/bugfix/resource-deletion
Browse files Browse the repository at this point in the history
fix resource deletion
  • Loading branch information
benwalch authored Aug 5, 2024
2 parents ae24475 + 4d4af0d commit b5c6dda
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Upgrade Notes

# 4.0.1

* fix resource deletion [#90](https://github.com/dachcom-digital/pimcore-dynamic-search/pull/90)

## Migrating from Version 3.x to Version 4.x

### Breaking Changes
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/PimcoreElementListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function onObjectPostUpdate(DataObjectEvent $event): void
/** @var DataObject\Concrete $object */
$object = $event->getObject();

// @deprecated since 5.0: published/unpublished must be handled by project-specific resource validation
$dispatchType = ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_UPDATE;
if (method_exists($object, 'isPublished') && $object->isPublished() === false) {
$dispatchType = ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_DELETE;
Expand Down Expand Up @@ -199,6 +200,7 @@ protected function checkInheritanceIndex(ElementInterface $element): void

foreach ($list->getObjects() as $childObject) {
$dispatchType = ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_UPDATE;
// @deprecated since 5.0: published/unpublished must be handled by project-specific resource validation
if (method_exists($childObject, 'isPublished') && $childObject->isPublished() === false) {
$dispatchType = ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_DELETE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Queue/DataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public function addToContextQueue(string $contextName, string $dispatchType, mix

protected function generateJob(string $contextName, string $dispatchType, mixed $resource, array $options): void
{

if($resource instanceof ElementInterface) {
// @todo: introduce generic "resource info" dto with resource information
if ($resource instanceof ElementInterface) {
$resourceType = sprintf('%s-%s', Element\Service::getElementType($resource), $resource->getId());
$resource = null;
} elseif (is_object($resource)) {
Expand Down
20 changes: 18 additions & 2 deletions src/Queue/MessageHandler/QueuedResourcesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
namespace DynamicSearchBundle\Queue\MessageHandler;

use DynamicSearchBundle\Builder\ContextDefinitionBuilderInterface;
use DynamicSearchBundle\Context\ContextDefinitionInterface;
use DynamicSearchBundle\Logger\LoggerInterface;
use DynamicSearchBundle\Normalizer\Resource\NormalizedDataResourceInterface;
use DynamicSearchBundle\Processor\Harmonizer\ResourceHarmonizerInterface;
use DynamicSearchBundle\Queue\Message\ProcessResourceMessage;
use DynamicSearchBundle\Queue\Message\QueueResourceMessage;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject;
use Pimcore\Model\Document;
use Pimcore\Model\Element;
use Symfony\Component\Messenger\Handler\Acknowledger;
use Symfony\Component\Messenger\Handler\BatchHandlerInterface;
Expand Down Expand Up @@ -42,11 +46,23 @@ private function process(array $jobs): void
try {

$resource = $message->resource;
// @todo: use introduced "resource info" dto to determinate resource / type
if (str_contains($message->resourceType, '-')) {
[$type, $id] = explode('-', $message->resourceType);
if (is_numeric($id)) {
$id = (int) $id;
}
$resource = Element\Service::getElementById($type, $id);
if (!$resource instanceof Element\ElementInterface) {
continue;
if ($resource === null && $message->dispatchType === ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_DELETE) {
// at this time, the resource is already deleted by pimcore
// since we do not serialize the resource into the message,
// we need to create a dummy resource to retrieve a valid resource meta for deletion
$resource = match ($type) {
'document' => new Document(),
'asset' => new Asset(),
'object' => new DataObject\Concrete(),
};
$resource->setId($id);
}
}

Expand Down

0 comments on commit b5c6dda

Please sign in to comment.