Skip to content

Instantly share code, notes, and snippets.

@timkelty
Created July 23, 2020 14:22
Show Gist options
  • Select an option

  • Save timkelty/0c97c9c0ec8e7984ef86ee04f7130a17 to your computer and use it in GitHub Desktop.

Select an option

Save timkelty/0c97c9c0ec8e7984ef86ee04f7130a17 to your computer and use it in GitHub Desktop.
<?php
namespace modules\appmodule\behaviors;
use Craft;
use craft\elements\Entry;
use craft\events\ModelEvent;
use craft\helpers\ElementHelper;
use yii\base\Behavior;
class Order extends Behavior
{
public function events()
{
return [
Entry::EVENT_BEFORE_SAVE => function (ModelEvent $event) {
$entry = $event->sender;
if (ElementHelper::isDraftOrRevision($entry)) {
return null;
}
// …do something before save: doesn't work
},
Entry::EVENT_AFTER_SAVE => function (ModelEvent $event) {
$entry = $event->sender;
if (!$event->isNew || ElementHelper::isDraftOrRevision($entry)) {
return null;
}
// …do something after save: it works
},
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment