Skip to content

Event for detecting Asset File Change #9717

Answered by brandonkelly
lukasIO asked this question in Q&A
Discussion options

You must be logged in to vote

You can check the asset’s scenario from EVENT_AFTER_SAVE to get an idea of what’s happening with the file.

use craft\elements\Asset;
use craft\events\ModelEvent;
use yii\base\Event;

Event::on(Asset::class, Asset::EVENT_AFTER_SAVE, function(ModelEvent $event) {
    /** @var Asset $asset */
    $asset = $event->sender;
    $scenario = $asset->getScenario();

    if (in_array($asset->getScenario(), [
        Asset::SCENARIO_CREATE,
        Asset::SCENARIO_MOVE,
        Asset::SCENARIO_REPLACE,
        Asset::SCENARIO_FILEOPS,
    ])) {
        // something is happening with the actual file
    }
});

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants