1

I have been implementing my own module like this;

protected function myFunction()
{
    Event::on(
    ElementQuery::class,
    ElementQuery::EVENT_BEFORE_PREPARE,
    function(Event $event) {

        if ($event->sender->elementType === 'craft\elements\Asset')
        {
            // DO SOMETHING...

However, I do not want to perform this function when an entry is saved. It seems that EVENT_BEFORE_PREPARE is triggered when I save an entry. So, is it possible to detect if the event is saving an entry or not?

1 Answer 1

1

Maybe you better use this kind of a event for your custom logic?

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

....

    Event::on(
        Entry::class,
        Entry::EVENT_BEFORE_SAVE,
        static function (ModelEvent $e) {
          
        }
    );

or

Event::on(
    Element::class,
    Element::EVENT_BEFORE_SAVE,
    static function (ModelEvent $$e) {
        // ...
    }
);
2
  • Actually, the function on EVENT_BEFORE_PREPARE has already been made, but a problem was found. So, I would like to prevent the function only if $event is saving an entry.
    – Kota.M
    Commented Oct 7, 2022 at 13:11
  • Not really sure what you want to do, but try looking at these options -> craftcms.com/docs/3.x/extend/events.html#event-code-generator i.imgur.com/zGP07xe.png Perhaps your problem is that every time you save the entry, you re-save the assets
    – RomanAvr
    Commented Oct 7, 2022 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.