diff --git a/lib/Event/AssetEvents.php b/lib/Event/AssetEvents.php index 4142790cef7..9ad64baddb7 100644 --- a/lib/Event/AssetEvents.php +++ b/lib/Event/AssetEvents.php @@ -115,6 +115,16 @@ final class AssetEvents */ const POST_DELETE_FAILURE = 'pimcore.asset.postDeleteFailure'; + /** + * Arguments: + * - params | array | contains the values that were passed to getById() as the second parameter + * + * @Event("Pimcore\Event\Model\AssetPreLoadEvent") + * + * @var string + */ + const PRE_LOAD = 'pimcore.asset.preLoad'; + /** * Arguments: * - params | array | contains the values that were passed to getById() as the second parameter diff --git a/lib/Event/DataObjectEvents.php b/lib/Event/DataObjectEvents.php index ce80fa31628..a5b749c2193 100644 --- a/lib/Event/DataObjectEvents.php +++ b/lib/Event/DataObjectEvents.php @@ -117,6 +117,16 @@ final class DataObjectEvents */ const POST_DELETE_FAILURE = 'pimcore.dataobject.postDeleteFailure'; + /** + * Arguments: + * - params | array | contains the values that were passed to getById() as the second parameter + * + * @Event("Pimcore\Event\Model\DataObjectPreLoadEvent") + * + * @var string + */ + const PRE_LOAD = 'pimcore.dataobject.preLoad'; + /** * Arguments: * - params | array | contains the values that were passed to getById() as the second parameter diff --git a/lib/Event/DocumentEvents.php b/lib/Event/DocumentEvents.php index a68780de7af..e88aff5893b 100644 --- a/lib/Event/DocumentEvents.php +++ b/lib/Event/DocumentEvents.php @@ -105,6 +105,16 @@ final class DocumentEvents */ const POST_DELETE_FAILURE = 'pimcore.document.postDeleteFailure'; + /** + * Arguments: + * - params | array | contains the values that were passed to getById() as the second parameter + * + * @Event("Pimcore\Event\Model\DocumentPreLoadEvent") + * + * @var string + */ + const PRE_LOAD = 'pimcore.document.preLoad'; + /** * Arguments: * - params | array | contains the values that were passed to getById() as the second parameter diff --git a/lib/Event/Model/AssetPreLoadEvent.php b/lib/Event/Model/AssetPreLoadEvent.php new file mode 100644 index 00000000000..62d0cda958b --- /dev/null +++ b/lib/Event/Model/AssetPreLoadEvent.php @@ -0,0 +1,58 @@ +asset = $asset; + $this->arguments = $arguments; + } + + public function getAsset(): Asset + { + if ($this->asset === null) { + throw new NotFoundException(); + } + return $this->asset; + } + + public function setAsset(?Asset $asset): void + { + $this->asset = $asset; + } + + public function getElement(): Asset + { + return $this->getAsset(); + } +} diff --git a/lib/Event/Model/DataObjectPreLoadEvent.php b/lib/Event/Model/DataObjectPreLoadEvent.php new file mode 100644 index 00000000000..56633f858af --- /dev/null +++ b/lib/Event/Model/DataObjectPreLoadEvent.php @@ -0,0 +1,57 @@ +object = $object; + $this->arguments = $arguments; + } + + public function getObject(): AbstractObject + { + if ($this->object === null) { + throw new NotFoundException(); + } + return $this->object; + } + + public function setObject(?AbstractObject $object): void + { + $this->object = $object; + } + + public function getElement(): AbstractObject + { + return $this->getObject(); + } +} diff --git a/lib/Event/Model/DocumentPreLoadEvent.php b/lib/Event/Model/DocumentPreLoadEvent.php new file mode 100644 index 00000000000..8a719f69ee7 --- /dev/null +++ b/lib/Event/Model/DocumentPreLoadEvent.php @@ -0,0 +1,57 @@ +document = $document; + $this->arguments = $arguments; + } + + public function getDocument(): Document + { + if ($this->document === null) { + throw new NotFoundException(); + } + return $this->document; + } + + public function setDocument(?Document $document): void + { + $this->document = $document; + } + + public function getElement(): Document + { + return $this->getDocument(); + } +} diff --git a/models/Asset.php b/models/Asset.php index bf7dbfa1c8e..c7645db3b8a 100644 --- a/models/Asset.php +++ b/models/Asset.php @@ -29,6 +29,7 @@ use Pimcore\Event\AssetEvents; use Pimcore\Event\FrontendEvents; use Pimcore\Event\Model\AssetEvent; +use Pimcore\Event\Model\AssetPreLoadEvent; use Pimcore\File; use Pimcore\Helper\TemporaryFileHelperTrait; use Pimcore\Loader\ImplementationLoader\Exception\UnsupportedException; @@ -247,6 +248,10 @@ public static function getById(int|string $id, array $params = []): ?static try { $asset->getDao()->getById($id); + // fire pre load event + $preLoadEvent = new AssetPreLoadEvent($asset, ['params' => $params]); + \Pimcore::getEventDispatcher()->dispatch($preLoadEvent, AssetEvents::PRE_LOAD); + $asset = $preLoadEvent->getAsset(); $className = \Pimcore::getContainer()->get('pimcore.class.resolver.asset')->resolve($asset->getType()); /** @var Asset $newAsset */ @@ -267,6 +272,14 @@ public static function getById(int|string $id, array $params = []): ?static $asset = null; } } else { + try { + // fire pre load event + $preLoadEvent = new AssetPreLoadEvent($asset, ['params' => $params]); + \Pimcore::getEventDispatcher()->dispatch($preLoadEvent, AssetEvents::PRE_LOAD); + $asset = $preLoadEvent->getAsset(); + } catch (NotFoundException $e) { + return null; + } RuntimeCache::set($cacheKey, $asset); } @@ -278,7 +291,7 @@ public static function getById(int|string $id, array $params = []): ?static } else { $asset = null; } - + /** @var ?static $asset */ return $asset; } diff --git a/models/DataObject/AbstractObject.php b/models/DataObject/AbstractObject.php index 9f9781a529d..efa4cd2a5b3 100644 --- a/models/DataObject/AbstractObject.php +++ b/models/DataObject/AbstractObject.php @@ -23,6 +23,7 @@ use Pimcore\Db; use Pimcore\Event\DataObjectEvents; use Pimcore\Event\Model\DataObjectEvent; +use Pimcore\Event\Model\DataObjectPreLoadEvent; use Pimcore\Logger; use Pimcore\Model; use Pimcore\Model\DataObject; @@ -242,6 +243,11 @@ public static function getById(int|string $id, array $params = []): ?static $object = self::getModelFactory()->build($className); RuntimeCache::set($cacheKey, $object); $object->getDao()->getById($id); + // fire pre load event + $preLoadEvent = new DataObjectPreLoadEvent($object, ['params' => $params]); + \Pimcore::getEventDispatcher()->dispatch($preLoadEvent, DataObjectEvents::PRE_LOAD); + $object = $preLoadEvent->getObject(); + $object->__setDataVersionTimestamp($object->getModificationDate()); Service::recursiveResetDirtyMap($object); @@ -261,6 +267,14 @@ public static function getById(int|string $id, array $params = []): ?static return null; } } else { + try { + // fire pre load event + $preLoadEvent = new DataObjectPreLoadEvent($object, ['params' => $params]); + \Pimcore::getEventDispatcher()->dispatch($preLoadEvent, DataObjectEvents::PRE_LOAD); + $object = $preLoadEvent->getObject(); + } catch (Model\Exception\NotFoundException $e) { + return null; + } RuntimeCache::set($cacheKey, $object); } @@ -272,7 +286,7 @@ public static function getById(int|string $id, array $params = []): ?static new DataObjectEvent($object, ['params' => $params]), DataObjectEvents::POST_LOAD ); - + /** @var ?static $object */ return $object; } diff --git a/models/Document.php b/models/Document.php index 4e2e0c976f0..791a1053918 100644 --- a/models/Document.php +++ b/models/Document.php @@ -21,6 +21,7 @@ use Pimcore\Event\DocumentEvents; use Pimcore\Event\FrontendEvents; use Pimcore\Event\Model\DocumentEvent; +use Pimcore\Event\Model\DocumentPreLoadEvent; use Pimcore\Logger; use Pimcore\Model\Document\Hardlink\Wrapper\WrapperInterface; use Pimcore\Model\Document\Listing; @@ -211,6 +212,10 @@ public static function getById(int|string $id, array $params = []): ?static try { $document->getDao()->getById($id); + // fire pre load event + $preLoadEvent = new DocumentPreLoadEvent($document, ['params' => $params]); + \Pimcore::getEventDispatcher()->dispatch($preLoadEvent, DocumentEvents::PRE_LOAD); + $document = $preLoadEvent->getDocument(); } catch (NotFoundException $e) { return null; } @@ -235,6 +240,14 @@ public static function getById(int|string $id, array $params = []): ?static \Pimcore\Cache::save($document, $cacheKey); } else { + try { + // fire pre load event + $preLoadEvent = new DocumentPreLoadEvent($document, ['params' => $params]); + \Pimcore::getEventDispatcher()->dispatch($preLoadEvent, DocumentEvents::PRE_LOAD); + $document = $preLoadEvent->getDocument(); + } catch (NotFoundException $e) { + return null; + } RuntimeCache::set($cacheKey, $document); } @@ -246,7 +259,7 @@ public static function getById(int|string $id, array $params = []): ?static new DocumentEvent($document, ['params' => $params]), DocumentEvents::POST_LOAD ); - + /** @var ?static $document */ return $document; }