Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification triggered when Craft saves draft #74

Closed
AdamChlan opened this issue Jun 6, 2022 · 7 comments
Closed

Notification triggered when Craft saves draft #74

AdamChlan opened this issue Jun 6, 2022 · 7 comments

Comments

@AdamChlan
Copy link

I'm curious if there is a way to not have a notification be sent when Craft saves a draft of the post in the admin. The plugin is working just as I need otherwise. Thank you.

@michtio
Copy link
Contributor

michtio commented Jun 6, 2022

Hey there 👋🏻

Thank you for using our plugin first of all.
To make sure the plugin doesn't send notifications when it's a draft you can add a conditional wrapper inside of the via() method.

You can use craft's element helper use craft\helpers\ElementHelper; and then simply do:

if (ElementHelper::isDraftOrRevision($entry)) {
            // don’t do anything with drafts or revisions
            return;
        }
        
        // ...

With the plugin example:

/**
 * Get the notification's delivery channels.
 *
 * @return array
 */
public function via()
{
    $entry = $this->event->sender;

   if (ElementHelper::isDraftOrRevision($entry)) {
      // don’t do anything with drafts or revisions
      return;
   }

    if ($entry->section->handle === 'blog' && !$this->event->isNew) {
        return [
            'database' => Craft::$app->getUsers()->getUserByUsernameOrEmail('hello@percipio.london'),
        ];
    }

    return [];
}

@AdamChlan
Copy link
Author

AdamChlan commented Jun 6, 2022

Thank you for the quick response. When I add this code, I get the following error message in my logs. I'll do some additional troubleshooting, but if you have any ideas, I'd appreciate it.

`2022-06-06 15:14website.test:39 [-][1819][i043i8idaebs4cqmmt9edq3vea][error][yii\base\ErrorException:2] yii\base\ErrorException: Invalid argument supplied for foreach() in /home/staging-snqfd/website.test/vendor/percipioglobal/craft-notifications/src/services/NotificationsService.php:89
Stack trace:

#0 /home/staging-snqfd/website.test/vendor/craftcms/cms/src/web/ErrorHandler.php(84): yii\base\ErrorHandler->handleError()

#1 /home/staging-snqfd/website.test/vendor/percipioglobal/craft-notifications/src/services/NotificationsService.php(89): craft\web\ErrorHandler->handleError()

#2 /home/staging-snqfd/website.test/vendor/percipioglobal/craft-notifications/src/Notifications.php(97): percipioglobal\notifications\services\NotificationsService->send()

#3 [internal function]: percipioglobal\notifications\Notifications->percipioglobal\notifications{closure}()

#4 /home/staging-snqfd/website.test/vendor/yiisoft/yii2/base/Event.php(312): call_user_func()

#5 /home/staging-snqfd/website.test/vendor/yiisoft/yii2/base/Component.php(642): yii\base\Event::trigger()

#6 /home/staging-snqfd/website.test/vendor/craftcms/cms/src/base/Element.php(4148): yii\base\Component->trigger()

#7 /home/staging-snqfd/website.test/vendor/craftcms/cms/src/elements/Entry.php(1680): craft\base\Element->afterSave()

#8 /home/staging-snqfd/website.test/vendor/craftcms/cms/src/services/Elements.php(2716): craft\elements\Entry->afterSave()

#9 /home/staging-snqfd/website.test/vendor/craftcms/cms/src/services/Elements.php(785): craft\services\Elements->_saveElementInternal()

#10 /home/staging-snqfd/website.test/vendor/craftcms/cms/src/services/Drafts.php(268): craft\services\Elements->saveElement()

#11 /home/staging-snqfd/website.test/vendor/craftcms/cms/src/controllers/EntryRevisionsController.php(169): craft\services\Drafts->saveElementAsDraft()

#12 [internal function]: craft\controllers\EntryRevisionsController->actionCreateDraft()

#13 /home/staging-snqfd/website.test/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array()

#14 /home/staging-snqfd/website.test/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams()

#15 /home/staging-snqfd/website.test/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction()

#16 /home/staging-snqfd/website.test/vendor/craftcms/cms/src/web/Application.php(293): yii\base\Module-

runAction()

#17 /home/staging-snqfd/website.test/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction()

#18 /home/staging-snqfd/website.test/vendor/craftcms/cms/src/web/Application.php(278): yii\web\Application->handleRequest()

#19 /home/staging-snqfd/website.test/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest()

#20 /home/staging-snqfd/website.test/public/index.php(24): yii\base\Application->run()`

@michtio
Copy link
Contributor

michtio commented Jun 7, 2022

@AdamChlan having an example of your code would be very helpful!

@AdamChlan
Copy link
Author

AdamChlan commented Jun 8, 2022

Sure, here is the entire file.

<?php

namespace app\notifications;

use Craft;
use craft\elements\Entry;
use craft\helpers\ArrayHelper;
use craft\helpers\ElementHelper;
use craft\mail\Message;
use percipioglobal\notifications\messages\SlackMessage;
use percipioglobal\notifications\models\Notification;

class r2rEventAdded extends Notification
{
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($event = null)
    {
        parent::__construct($event);
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array
     */
    public function via()
    {
        $entry = $this->event->sender;

        if (ElementHelper::isDraftOrRevision($entry)) {
            return;
        }

        if ($entry->section->handle === 'events' && $entry->type->handle === 'r2r' && $this->event->isNew) {
            return [
                'mail' => ['email-address-removed'],
            ];
        }
        return [];
    }

    public function toDatabase()
    {
        return ArrayHelper::toArray($this);
    }

    public function toSlack()
    {
        return (new SlackMessage)
            ->content("Hello world.");
    }

    public function toMail($notifiable)
    {
        $title = $this->event->sender->title;

        $message = new Message();
        $message->setTo($notifiable);
        $message->setSubject("New R2R submission from website");
        $message->setHtmlBody("<p>Please log in to the website and review the new R2R event that has been submitted.</p> <p>Title: {$title}</p>");

        return $message;
    }
}

@michtio
Copy link
Contributor

michtio commented Jun 9, 2022

Thank you @AdamChlan for the code example, we'll set it up and see if we encounter any issues and do a bug fix if there is something off.

@AdamChlan
Copy link
Author

Hi @michtio , I was curious if you ever had a chance to test this out?

@cookie10codes
Copy link
Contributor

cookie10codes commented Aug 19, 2022

hi @AdamChlan,

Sorry that we didn't reply earlier on your message. Can you try this

Return an empty array in the via method (so update the return to return []) <-- this was our bad

if (ElementHelper::isDraftOrRevision($entry)) {
      return [];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants