Skip to content

Commit

Permalink
feat: added before load event for objects, documents and assets
Browse files Browse the repository at this point in the history
  • Loading branch information
zoidbergx committed Oct 17, 2023
1 parent 7b637e5 commit 949d78a
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Event/AssetEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/Event/DataObjectEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/Event/DocumentEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions lib/Event/Model/AssetPreLoadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Event\Model;

use Pimcore\Event\Traits\ArgumentsAwareTrait;
use Pimcore\Model\Asset;
use Pimcore\Model\Exception\NotFoundException;
use Symfony\Contracts\EventDispatcher\Event;

class AssetPreLoadEvent extends Event implements ElementEventInterface
{
use ArgumentsAwareTrait;

protected ?Asset $asset;

/**
* AssetEvent constructor.
*
* @param array $arguments additional parameters (e.g. "versionNote" for the version note)
*/
public function __construct(?Asset $asset, array $arguments = [])
{
$this->asset = $asset;
$this->arguments = $arguments;
}

public function getAsset(): ?Asset
{
if (empty($this->document)) {
throw new NotFoundException();
}
return $this->asset;
}

public function setAsset(?Asset $asset): void
{
$this->asset = $asset;
}

public function getElement(): Asset
{
return $this->getAsset();
}
}
57 changes: 57 additions & 0 deletions lib/Event/Model/DataObjectPreLoadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Event\Model;

use Pimcore\Event\Traits\ArgumentsAwareTrait;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\Exception\NotFoundException;
use Symfony\Contracts\EventDispatcher\Event;

class DataObjectPreLoadEvent extends Event implements ElementEventInterface
{
use ArgumentsAwareTrait;

protected ?AbstractObject $object;

/**
* DataObjectEvent constructor.
*
*/
public function __construct(?AbstractObject $object, array $arguments = [])
{
$this->object = $object;
$this->arguments = $arguments;
}

public function getObject(): ?AbstractObject
{
if (empty($this->object)) {
throw new NotFoundException();
}
return $this->object;
}

public function setObject(?AbstractObject $object): void
{
$this->object = $object;
}

public function getElement(): AbstractObject
{
return $this->getObject();
}
}
57 changes: 57 additions & 0 deletions lib/Event/Model/DocumentPreLoadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Event\Model;

use Pimcore\Event\Traits\ArgumentsAwareTrait;
use Pimcore\Model\Document;
use Pimcore\Model\Exception\NotFoundException;
use Symfony\Contracts\EventDispatcher\Event;

class DocumentPreLoadEvent extends Event implements ElementEventInterface
{
use ArgumentsAwareTrait;

protected ?Document $document;

/**
* DocumentEvent constructor.
*
*/
public function __construct(?Document $document, array $arguments = [])
{
$this->document = $document;
$this->arguments = $arguments;
}

public function getDocument(): ?Document
{
if (empty($this->document)) {
throw new NotFoundException();
}
return $this->document;
}

public function setDocument(?Document $document): void
{
$this->document = $document;
}

public function getElement(): Document
{
return $this->getDocument();
}
}
7 changes: 7 additions & 0 deletions models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

use Doctrine\DBAL\Exception\DeadlockException;
use Exception;
use Pimcore\Event\DocumentEvents;
use Pimcore\Event\Model\DocumentPreLoadEvent;
use function is_array;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
Expand All @@ -29,6 +31,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;
Expand Down Expand Up @@ -247,6 +250,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 */
Expand Down
6 changes: 6 additions & 0 deletions models/DataObject/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 949d78a

Please sign in to comment.