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

PHP Warning - yy\base\ErrorException after user perms save #17

Closed
giacomocarra opened this issue Jan 24, 2019 · 4 comments
Closed

PHP Warning - yy\base\ErrorException after user perms save #17

giacomocarra opened this issue Jan 24, 2019 · 4 comments

Comments

@giacomocarra
Copy link

Hi,
I installed the plugin and used it as expected until now.
Yesterday I performed the 3.1 Craft update and then I tried to save a pre-existing user permission from Craft backend.
This is what I get (see the log below or the pdf attached). Even refreshing the page, the error persist, and the changes aren't applied.
Any idea to solve this?
Thank you

Log:

yii\base\ErrorException: Invalid argument supplied for foreach() in /var/www/copernit/vendor/rias/craft-notifications/src/services/NotificationsService.php:89
Stack trace:
#0 /var/www/copernit/vendor/craftcms/cms/src/web/ErrorHandler.php(76): yii\base\ErrorHandler->handleError(2, 'Invalid argumen...', '/var/www/copern...', 89)
#1 /var/www/copernit/vendor/rias/craft-notifications/src/services/NotificationsService.php(89): craft\web\ErrorHandler->handleError(2, 'Invalid argumen...', '/var/www/copern...', 89, Array)
#2 /var/www/copernit/vendor/rias/craft-notifications/src/Notifications.php(97): rias\notifications\services\NotificationsService->send(Object(app\notifications\UserSubscribed))
#3 [internal function]: rias\notifications\Notifications->rias\notifications{closure}(Object(craft\events\ModelEvent))
#4 /var/www/copernit/vendor/yiisoft/yii2/base/Event.php(310): call_user_func(Object(Closure), Object(craft\events\ModelEvent))
#5 /var/www/copernit/vendor/yiisoft/yii2/base/Component.php(636): yii\base\Event::trigger('craft\elements\...', 'afterSave', Object(craft\events\ModelEvent))
#6 /var/www/copernit/vendor/craftcms/cms/src/base/Element.php(1882): yii\base\Component->trigger('afterSave', Object(craft\events\ModelEvent))
#7 /var/www/copernit/vendor/craftcms/cms/src/elements/User.php(1323): craft\base\Element->afterSave(false)
#8 /var/www/copernit/vendor/craftcms/cms/src/services/Elements.php(515): craft\elements\User->afterSave(false)
#9 /var/www/copernit/vendor/craftcms/cms/src/controllers/UsersController.php(1042): craft\services\Elements->saveElement(Object(craft\elements\User), false)
#10 [internal function]: craft\controllers\UsersController->actionSaveUser()
#11 /var/www/copernit/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#12 /var/www/copernit/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#13 /var/www/copernit/vendor/craftcms/cms/src/web/Controller.php(109): yii\base\Controller->runAction('save-user', Array)
#14 /var/www/copernit/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('save-user', Array)
#15 /var/www/copernit/vendor/craftcms/cms/src/web/Application.php(297): yii\base\Module->runAction('users/save-user', Array)
#16 /var/www/copernit/vendor/craftcms/cms/src/web/Application.php(561): craft\web\Application->runAction('users/save-user', Array)
#17 /var/www/copernit/vendor/craftcms/cms/src/web/Application.php(281): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#18 /var/www/copernit/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#19 /var/www/copernit/web/index.php(21): yii\base\Application->run()
#20 {main}
yii_base_ErrorException.pdf

@riasvdv
Copy link

riasvdv commented Jan 25, 2019

Could you post your notifications.php config file and the code you're using for the notification?

@giacomocarra
Copy link
Author

Sure!

notifications.php

<?php

return [
    /**
     * This is where you define your notifications for example if you want to trigger a notification
     * every time a blogpost is added, your notifications array could look like this:
    * 'notifications' => [
    *     [
    *         'elementType' => \craft\elements\Entry::class,
    *         'event' => \craft\elements\Entry::EVENT_AFTER_SAVE,
    *         'criteria' => [
    *             'section' => 'blog'
    *         ],
    *         'notification' => \app\notifications\BlogPostAdded::class,
    *     ],
    * ],
     *
     * In your BlogPostAdded class' via($event) function you could then check if the entry that was saved is a new entry
     * through the event properties
     */
    'notifications' => [
        [
            'class' => \craft\elements\User::class,
            'event' => \craft\elements\User::EVENT_AFTER_SAVE,
            'notification' => \app\notifications\UserSubscribed::class,
        ],
    ],
];

UserSubscribed.php
(this file is placed in the notification folder, in the root)

<?php

namespace app\notifications;

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


class UserSubscribed 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()
    {
        if( $this->event->isNew ) {
            return [ 'mail' => 'giacomo.carra@paxxion.com' ];
        }

        return true;
    }

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

    public function toSlack()
    {
        return (new SlackMessage)
            ->content("Nuova richiesta utente");
    }

    public function toMail($notifiable)
    {
        $message = new Message();
        $message->setTo($notifiable);
        $message->setSubject("Nuova richiesta utente dal sito");
        $message->setHtmlBody("
            <p>Hey!</p>
            <p>Un nuovo utente ha richiesto l'accesso all'area riservata.</p>
            <p><a href=\"http://copernit.local/admin/login\">Controlla il pannello di amministrazione</a> del sito web per vedere di cosa si tratta.</p>
        ");

        return $message;
    }
}

Thank you

@riasvdv
Copy link

riasvdv commented Jan 25, 2019

So I think the issue is that your via() method returns true instead of an array, if you don't want to send the notification it's best to return []

That should solve the error

@giacomocarra
Copy link
Author

Thank you, the problem seems to be fixed!
However I'm experiencing some issues with Craft 3.1.4 and the mailer settings (also the simple contact form made by Pixel & Tonic stopped working), so it doesn't send either contact mail and notifications. I'll try to debug it, maybe there's something wrong with my code but this is another story.
Thank you again for your help ;)

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

2 participants