1

We are still trying to make a custom mail plugin. We have one problem left. When someone makes a draft, and later publishes it, only the onSaveEntry fires but without NewEntry. So we were trying to use the onPublishDraft instead. But we can't get it to fire. I'm using Craft CMS 2.6.3 Pro

this is what we tried so far:

craft()->on('entryRevisions.onPublishDraft', function(Event $event){}
craft()->on('entryRevisions.publishDraft', function(Event $event){}
craft()->on('publishDraft', function(Event $event){}
craft()->entryRevisions->onPublishDraft = function(Event $event) {}
craft()->entryRevisions->publishDraft = function(Event $event) {}

this is the regular on save entry we use, maybe there is another way to catch a draft that is being published;

craft()->on('entries.saveEntry', function(Event $event){ 
if ($event->params['isNewEntry'] && $entry->status == 'live'){    }});

Hope you guys know how to do that

UPDATE

I did do a test with the exact code Brad gave, but it still didn't do anything... This is how my test starts;

public function init(){
    parent::init();

    craft()->on('entryRevisions.onPublishDraft', function(Event $event) {
        Craft::log("draft published", LogLevel::Info, true);
    });

    craft()->on('entries.saveEntry', function(Event $event) {
        Craft::log("Entry saved", LogLevel::Info, true);
    });
}

Only the second one gets logged when I change the status of the post, the first never.

*****************************************
2017/12/21 21:29:54 [info] [application] [Forced] Entry saved
*****************************************
2

Maybe you already solved your issue but I wanted to explain what I did the last time... You can make use of the craft_content table and it's implementation in Craft 2 that fetches and includes all columns no matter of the field layout.

Make use of the light switch field, if you already have one that you are not going to use in your specific entry type you can abuse that, otherwise create a new one. You don't want to include this field into your field layout, you'll only work with it in your php code. Instead of looking if your entry is a new entry you are going to check if that field is set to true or false. You can change the field however you like, it is saved correctly and populated correctly, but your clients won't be able to change it manually (so they can't resend the mail by mistake.

Below is the code I used, my requirement was to send the mail as soon as the entry was activated (they were saved as disabled from a frontend form). Since you asked in a comment how to check when an entry goes from disabled to enabled I thought it might be helpful.

craft()->on('entries.onBeforeSaveEntry', function(Event $event) {
        if(!$event->params['isNewEntry']){
            /* @var $entry EntryModel */
            $entry = $event->params['entry'];
            if($entry->section->handle === "projects"){
                $oldEntry = craft()->entries->getEntryById($entry->id);
                // check if the entry was disabled and is now enabled and if no mail was already send
                if($oldEntry->enabled === false && $entry->enabled === true && $entry->getContent()->getAttribute('mailSended') === 0){
                    $originalPath = craft()->templates->getTemplatesPath();
                    craft()->templates->setTemplatesPath(CRAFT_TEMPLATES_PATH);

                    $html = craft()->templates->render('email/type/activateProject.twig', array(
                        'entry' => $entry,
                        'userFullName'  => $entry->getContent()->getAttribute('firstName') . " " . $entry->getContent()->getAttribute('lastName')
                    ));

                    if($html){
                        craft()->rodach->sendMail($html, Craft::t('email_subject_activate_entry'), $entry->eMail);
                        $entry->getContent()->setAttribute('mailSended', 1);
                    }

                    craft()->templates->setTemplatesPath($originalPath);
                }
            }
        }
    });
|improve this answer|||||
0

Just did a test in a simple plugin with this in its init() method, then published a draft and it worked, so guessing it's some other logic in your plugin that is failing.

craft()->on('entryRevisions.onPublishDraft', function(Event $event) {
    // ...
});
|improve this answer|||||
  • I tried that, but it still didn't do anything, I've commented every other line except the line that logs a simple text. So I'm assuming that it can't be any other line... See my updated question. – Brandon Dec 21 '17 at 21:43
  • Huh... weird. Any chance you've got other plugins installed listening to onPublishDraft? Maybe try disabling them? – Brad Bell Dec 21 '17 at 21:47
  • I Just found out that there is a difference between a draft and a disabled entry. I honestly thought they were the same thing. My question changes then... is there an event that fires when the status of an entry goes from disabled to enable, when saving an entry? – Brandon Dec 21 '17 at 23:12

Your Answer

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

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