craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.27k stars 635 forks source link

Add EVENT_DELETE_CREATED_TRANSFORMS in AssetTransforms.php #2212

Closed khalwat closed 6 years ago

khalwat commented 6 years ago

It'd be wonderful if the deleteCreatedTransformsForAsset() fired an EVENT_DELETE_CREATED_TRANSFORMS so that plugins that may have created image variants via EVENT_GENERATE_TRANSFORM have a chance to clean up after themselves.

Specifically in my case, I create an .webp variant, and I'd like to clean it up if transforms are deleted as per:

https://github.com/nystudio107/craft3-imageoptimize/issues/22

khalwat commented 6 years ago

Thanks @andris-sevcenko but (there's always a "but")...

In order to be able to find the proper transform sub-folder, we'd need to have $transformIndex passed down too:

        // Fire a 'deleteCreatedTransforms' event
        if ($this->hasEventHandlers(self::EVENT_DELETE_ASSET_TRANSFORMS)) {
            $this->trigger(self::EVENT_DELETE_ASSET_TRANSFORMS, new DeleteAssetTransformsEvent([
                'asset' => $asset,
                'transformIndex' => $transformIndex,
            ]));

...otherwise we won't be able to do:

...in the event handler (we'll have no idea what transformIndex is associated with the passed down asset).

brandonkelly commented 6 years ago

@khalwat Just made some changes to the event(s). Let me know if it will work for you now.

andris-sevcenko commented 6 years ago

@khalwat yeah for some reason I had assumed that nystudio107/craft3-imageoptimize maintained it's own transform index and would know what files to get rid of. I'm not sure why, now.