Asset save with argument runs into a infintie loop

Expected behavior

As a user, I should be able to hook the asset event and able to update the asset with an argument.

Actual behavior

When I tried to hook an asset event and update the object by passing some version notes to it and the program went into an infinite loop. I can able to reproduce this issue on the master branch.

Steps to reproduce

Add the postupdate asset event in service.yml
In the method of the hook, call to save with argument.

Code example

            - { name: kernel.event_listener, event: pimcore.asset.postUpdate, method: onPostUpdateAsset}

method: onPostUpdateAsset logic

public function onPostUpdateAsset(ElementEventInterface $e)
        if (($e instanceof AssetEvent) && ($e->getAsset()->getType() != "folder") && $e->getArguments() == null ) {
            $assetObj = $e->getAsset();
            $assetObj->save(['versionNote' => "Adding property"]);

It’s quite simple actually. Because you call ->save() again the whole chain is executed again, including your event listener, which then repeats itself infinitely. You should use the pimcore.asset.preUpdate event instead and not use ->save().

1 Like

@kjkooistra-youwe Thanks for the response. Here my use-case needs to point on postUpdate only and also to avoid the infinite loop I’m passing some arguments to save so that I can stop the loop. But that arguments are not getting on $e->getArguments().

If you use the preUpdate event you can do:

$this->setArgument('versionNote', 'Adding property');

Which is then passed on to saveVersion, which also triggers the same event listeners with argument 'saveVersionOnly' => true.

1 Like

Making sense.

@kjkooistra-youwe But why that argument is not passed through to postUpdate call. I would like to do one operation based on that value.