Start free trial

Extensions (PHP) · Events and listeners

Events and listeners

Listeners run your code when something happens on the forum, like a new topic or a ban. Make one with fs:make listener, or register a closure in your class.

A listener class

namespace Harbour\MemberMap\Listeners;

use App\Events\WebhookEvent;
use App\Extensions\Contracts\Listener;

class WelcomeNewMember implements Listener
{
    public function topic(): string
    {
        return 'users/create';
    }

    public function handle(WebhookEvent $event): void
    {
    }
}

In your class

$this->listen('topics/create', function ($event) {
});

$this->listenLater('replies/create', CountReplies::class);

listen runs straight away. listenLater sends the work to the extension queue, which is the right choice for anything slow, like calling another website.

What you can listen to

EventWhen
topics/createWhen a topic is created
topics/updateWhen a topic is edited
topics/deleteWhen a topic is deleted
topics/lockWhen a topic is locked
topics/unlockWhen a topic is unlocked
topics/pinWhen a topic is pinned
topics/unpinWhen a topic is unpinned
topics/moveWhen a topic is moved to another forum
topics/mergeWhen a topic is merged into another
topics/approveWhen a topic waiting for approval is approved
replies/createWhen a reply is posted
replies/updateWhen a reply is edited
replies/deleteWhen a reply is deleted
replies/approveWhen a reply waiting for approval is approved
replies/moveWhen replies are moved to another topic
reactions/addWhen someone reacts
reactions/removeWhen a reaction is taken back
users/createWhen a member joins
users/updateWhen a member is changed
users/deleteWhen a member is deleted
users/banWhen a member is banned
users/loginWhen a member signs in
groups/createWhen a group is created
groups/updateWhen a group is changed
groups/deleteWhen a group is deleted
reports/createWhen something is reported
credits/transactionWhen credits change hands

The admin panel's Developer, Core events page shows what each event gives your code, like the topic and the member.

Saving and deleting

Every kind of content and member record has six events: before and after it is created, changed or deleted. Add the moment to the name, like topics/creating or blog-posts/deleted. Your listener gets the record itself.

RecordName in code
Topictopics/creating, updating, deleting, created, updated, deleted
Replyreplies/creating, updating, deleting, created, updated, deleted
Pollpolls/creating, updating, deleting, created, updated, deleted
Poll votepoll-votes/creating, updating, deleting, created, updated, deleted
Reactionreactions/creating, updating, deleting, created, updated, deleted
Attachmentattachments/creating, updating, deleting, created, updated, deleted
Tagtags/creating, updating, deleting, created, updated, deleted
Forumforums/creating, updating, deleting, created, updated, deleted
Labellabels/creating, updating, deleting, created, updated, deleted
Memberusers/creating, updating, deleting, created, updated, deleted
Groupgroups/creating, updating, deleting, created, updated, deleted
Status updatestatus-updates/creating, updating, deleting, created, updated, deleted
Status replystatus-replies/creating, updating, deleting, created, updated, deleted
Conversationconversations/creating, updating, deleting, created, updated, deleted
Private messagemessages/creating, updating, deleting, created, updated, deleted
Shoutshouts/creating, updating, deleting, created, updated, deleted
Reportreports/creating, updating, deleting, created, updated, deleted
Warningwarnings/creating, updating, deleting, created, updated, deleted
Banbans/creating, updating, deleting, created, updated, deleted
Member notemember-notes/creating, updating, deleting, created, updated, deleted
Name change requestname-changes/creating, updating, deleting, created, updated, deleted
Eventevents/creating, updating, deleting, created, updated, deleted
Event RSVPevent-attendees/creating, updating, deleting, created, updated, deleted
Arcade scorearcade-scores/creating, updating, deleting, created, updated, deleted
Showcase entryshowcase-entries/creating, updating, deleting, created, updated, deleted
Showcase reviewshowcase-reviews/creating, updating, deleting, created, updated, deleted
Shop purchaseshop-purchases/creating, updating, deleting, created, updated, deleted
Blog postblog-posts/creating, updating, deleting, created, updated, deleted
Gallery albumgallery-albums/creating, updating, deleting, created, updated, deleted
Gallery itemgallery-items/creating, updating, deleting, created, updated, deleted
Gallery commentgallery-comments/creating, updating, deleting, created, updated, deleted
Questionquestions/creating, updating, deleting, created, updated, deleted
Marketplace productmarketplace-products/creating, updating, deleting, created, updated, deleted
Marketplace ordermarketplace-orders/creating, updating, deleting, created, updated, deleted
Marketplace reviewmarketplace-reviews/creating, updating, deleting, created, updated, deleted
Clubclubs/creating, updating, deleting, created, updated, deleted

Changing or refusing a save

The creating, updating and deleting events run during the save, before anything is stored. Change the record, or stop the save with a message the member sees:

$this->listen('topics/creating', function (Topic $topic) {
    if (str_contains(strtolower($topic->title), 'free money')) {
        $this->refuse('Topics about free money are not allowed here.', 'title');
    }
});

Keep them quick and use listen, not listenLater: they run while the member waits. If your listener fails, ForumSpark logs it and saves anyway, so a bug in your extension never stops members from posting. A listener class for these implements App\Extensions\Contracts\BeforeSave with handle(Model $record).

The created, updated and deleted events run for every save, also imports and moves. To react to a member posting a topic, use topics/create above: it runs once the topic and its first post are both saved.

System events

These are about the forum itself rather than a record. Some let you change what happens by returning something, as described.

EventWhen
request/startingWhen a forum page or API request starts. Gets the request; return a response to answer it yourself
request/finishedWhen a forum page or API request has its response. Gets the request and the response; return a response to replace it
mail/sendingBefore an email leaves the forum. Gets the email message, which you can change
notifications/sendingBefore a notification is sent. Gets the member, the notification and the channel; return false to stop it
formatter/configureWhen the post formatter is set up. Gets the s9e TextFormatter configurator, to add BBCode or tags
users/logoutWhen a member signs out. Gets the member
users/verifyWhen a member confirms their email address. Gets the member
users/password-resetWhen a member sets a new password with a reset link. Gets the member

Scheduled tasks

fs:make task CleanUp --every=daily adds a job that runs on a schedule, from every minute to once a day. It is listed in extension.json and runs only on forums where the extension is on.