XF 2.2 Using the alert handler...

Lee

Well-known member
I have setup the following content type for the alert handler class:

1641423288110.png

I then have this protected php function:

PHP:
    protected function sendAlert($alertType, $to, $fromUser)
    {
        /* Load the Alert Repo */
        $alertRepo = $this->repository('XF:UserAlert');

        $alertRepo->alert(
            $to,
            $fromUser->user_id,
            $fromUser->username,
            'tls_thoughts',
            '',
            $alertType,
            [],
            ['autoRead' => true]
        );
    }

and then this code actually send the alert:

PHP:
        $thoughtsWatching = $this->finder('TLS\Thoughts:ThoughtWatch');
        $thoughtsWatchingResult = $thoughtsWatching->fetch();

        $fromUser = \XF::visitor();

        foreach($thoughtsWatchingResult as $thoughtsWatchingData)
        {
            $user = \XF::em()->find('XF:User', $thoughtsWatchingData->user_id);
            /* Send an alert on Submission */
            if($thoughtsWatchingData->user_id != \XF::visitor()->user_id)
            {
            $this->sendAlert('new_thought', $user, $fromUser);
            }
        }

I also have a template with this name: alert_user_alert_tls_new_thought, which I think is named correctly...? I also had this working at one point but don't recall what I changed that broke it.

The alert is being added to the database and the bubble is showing the alert as sent correctly, but then showing no new alerts when accessed.

Does anybody please have any ideas what is wrong?
 

Chris D

XenForo developer
Staff member
I think this will be leaving a clue in the server error log. If it isn’t, if you navigate to the Alerts page directly (not the popup) it should have a message displayed on screen.
 

Xon

Well-known member
You are sending with the content_type tls_thoughts, so the template should be; alert_tls_thoughts_new_thought.

UserAlert::canView returns false if there is no content, so TLS\Thoughts\Alert\Thought::getContent will need to return an empty entity (with a canView method that returns true).

Alternative; you can just use the content type user with the custom action and re-use the existing User alert handler.

The alert should also be link to your add-on via the dependsOnAddOnId attribute in the $options argument (ie the one with autoRead)
 

Lee

Well-known member
I think this will be leaving a clue in the server error log. If it isn’t, if you navigate to the Alerts page directly (not the popup) it should have a message displayed on screen.
You are sending with the content_type tls_thoughts, so the template should be; alert_tls_thoughts_new_thought.

UserAlert::canView returns false if there is no content, so TLS\Thoughts\Alert\Thought::getContent will need to return an empty entity (with a canView method that returns true).

Alternative; you can just use the content type user with the custom action and re-use the existing User alert handler.

The alert should also be link to your add-on via the dependsOnAddOnId attribute in the $options argument (ie the one with autoRead)
Thanks to you both.

I have changed the content_type to user as @Xon recommended and linked it to my addon using dependsOnAddOnId and all is now working.
 

Lee

Well-known member
Just in case anybody was curious, this is the addon I am working on - a way for users to share their thoughts in a journal.

Still very much a work in progress, but a good learning exercise regardless.
 
Top