I want to send a Mail to an admin, whenever a User of group ‚Cook‘ creates an Entry of Type ‚Recipe‘.
For that I created an Event reacting on Entry::EVENT_AFTER_SAVE :
Event::on(
Entry::class,
Entry::EVENT_AFTER_SAVE, static function (ModelEvent $event) {
/** Entry @entry */
$entry = $event->sender;
/** @var User $user */
$user = Craft::$app->getUser();
if (!$event->isNew) {
if ($user->identity->isInGroup('cook') &&
ElementHelper::isDraftOrRevision($entry) &&
$entry->getStatus() === Entry::STATUS_DISABLED) {
$cpUrl = $entry->cpEditUrl;
$bodyText = "Dear Admin. /n $userName has created <a href=$cpUrl>$entry</a>.";
};
}
});
As your can see - I tried a lot to only react on the first creation of the entry. But all this does not the trick. Behind the scenes craft created an Entry (and saves it initially) even when the Cook presses the ’New Entry’ Button.
So I don’t know hook in only when the message is first saved directly by the user pressing the ‚Create‘ Button after entering the content.
It would be nice if your could help me with that.
Thanks in advance. Michael