Halting Execution Based on Events

Certain events will let you control whether further execution occurs within concrete5. You can control this by setting properties within the event objects themselves. For example, check out the on_before_user_add event. This event fires when an add request occurs in concrete5, but before the user is actually added. You can halt the add process here.

The $event object in the example below is that of the Concrete\Core\User\Event\AddUser object.

Events::addListener('on_before_user_add', function($event) {
    $data = $event->getData();
    if ($data['uName'] == 'andrew') {
        // We don't want to allow any users named andrew.
        $event->cancelAdd();
    }
});

The AddUser event object has a cancelAdd() method, which sets an internal boolean named $proceed to false. This boolean is then checked within the concrete5 core code prior to continuing. If it has been cancelled, the add operation will not continue.

Loading Conversation