1

I'm looking to hook into an Asset::EVENT_BEFORE_HANDLE_FILE event and toss up a JS flash error if it's above a certain file size. However, the registerJs() function never runs and I assume it's because this is some sort of anti-pattern. What is the more proper way to achieve this sort of thing?

Event::on(
    Asset::class,
    Asset::EVENT_BEFORE_HANDLE_FILE,
    function (Event $event) {
        $asset = $event->asset;

        if (filesize($asset->tempFilePath) > 100000) {
            Craft::$app->getView()->registerJs('Craft.cp.displayError("Example error message")');
        }
    }
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.