# RolesEvent events

> 7 FeatherPanel plugin events on RolesEvent (3 emitted by the core): the event names, the arguments each listener gets, where they fire and a PHP listener for each.

Source: https://featherpanel.com/docs/plugins/events/roles/

Part of the [plugin events reference](https://featherpanel.com/docs/plugins/events/). Listeners get the payload values as positional arguments in the order shown, cannot cancel the action, and must catch their own exceptions. 4 of these events are declared but no code emits them yet; they are marked not emitted.

## Setup

Import `RolesEvent` in your entry class and register listeners in `processEvents()`:

```php
<?php

namespace App\Addons\myplugin;

use App\App;
use App\Plugins\AppPlugin;
use App\Plugins\PluginEvents;
use App\Plugins\Events\Events\RolesEvent;

class MyPlugin implements AppPlugin
{
    public static function processEvents(PluginEvents $event): void
    {
        $event->on(RolesEvent::onRoleDeleted(), function ($role, $deletedBy): void {
            App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:roles:role:deleted ' . json_encode(['role' => $role, 'deleted_by' => $deletedBy]));
        });
    }

    public static function pluginInstall(): void
    {
    }

    public static function pluginUninstall(): void
    {
    }
}
```

## `RolesEvent::onRolesError()`

- Name: `featherpanel:admin:roles:error`
- not emitted
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/RolesEvent.php#L69)

Docblock: string error message, array context.

```php
$event->on(RolesEvent::onRolesError(), function (...$args): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:roles:error ' . json_encode($args));
});
```

No code emits this event yet, so the arguments are unknown. The listener accepts any.

## `RolesEvent::onRolesRetrieved()`

- Name: `featherpanel:admin:roles:retrieved`
- not emitted
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/RolesEvent.php#L28)

Docblock: array roles list.

```php
$event->on(RolesEvent::onRolesRetrieved(), function (...$args): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:roles:retrieved ' . json_encode($args));
});
```

No code emits this event yet, so the arguments are unknown. The listener accepts any.

## `RolesEvent::onRoleCreated()`

- Name: `featherpanel:admin:roles:role:created`
- emitted at 1 site
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/RolesEvent.php#L44)

Docblock: array role data. (the emitter passes 2 values, see the table)

| # | Payload key | Listener parameter | Type |
| --- | --- | --- | --- |
| 1 | `role` | `$role` | not declared |
| 2 | `created_by` | `$createdBy` | not declared |

When it fires (1 site)

-   [`RolesController::create`](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Controllers/Admin/RolesController.php#L311)[`PUT /api/admin/roles`](https://featherpanel.com/docs/api/reference/admin/roles/#put-admin-roles)

```php
$event->on(RolesEvent::onRoleCreated(), function ($role, $createdBy): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:roles:role:created');
});
```

## `RolesEvent::onRoleDeleted()`

- Name: `featherpanel:admin:roles:role:deleted`
- emitted at 1 site
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/RolesEvent.php#L60)

Docblock: int role id, array role data.

| # | Payload key | Listener parameter | Type |
| --- | --- | --- | --- |
| 1 | `role` | `$role` | `int`from docblock |
| 2 | `deleted_by` | `$deletedBy` | `array`from docblock |

When it fires (1 site)

-   [`RolesController::delete`](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Controllers/Admin/RolesController.php#L514)[`DELETE /api/admin/roles/{id}`](https://featherpanel.com/docs/api/reference/admin/roles/#delete-admin-roles-id)

```php
$event->on(RolesEvent::onRoleDeleted(), function ($role, $deletedBy): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:roles:role:deleted ' . json_encode(['role' => $role, 'deleted_by' => $deletedBy]));
});
```

## `RolesEvent::onRoleNotFound()`

- Name: `featherpanel:admin:roles:role:not:found`
- not emitted
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/RolesEvent.php#L77)

Docblock: int role id, string error message.

```php
$event->on(RolesEvent::onRoleNotFound(), function (...$args): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:roles:role:not:found ' . json_encode($args));
});
```

No code emits this event yet, so the arguments are unknown. The listener accepts any.

## `RolesEvent::onRoleRetrieved()`

- Name: `featherpanel:admin:roles:role:retrieved`
- not emitted
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/RolesEvent.php#L36)

Docblock: int role id, array role data.

```php
$event->on(RolesEvent::onRoleRetrieved(), function (...$args): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:roles:role:retrieved ' . json_encode($args));
});
```

No code emits this event yet, so the arguments are unknown. The listener accepts any.

## `RolesEvent::onRoleUpdated()`

- Name: `featherpanel:admin:roles:role:updated`
- emitted at 1 site
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/RolesEvent.php#L52)

Docblock: int role id, array old data, array new data.

| # | Payload key | Listener parameter | Type |
| --- | --- | --- | --- |
| 1 | `role` | `$role` | `int`from docblock |
| 2 | `updated_data` | `$updatedData` | `array`from docblock |
| 3 | `updated_by` | `$updatedBy` | `array`from docblock |

When it fires (1 site)

-   [`RolesController::update`](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Controllers/Admin/RolesController.php#L443)[`PATCH /api/admin/roles/{id}`](https://featherpanel.com/docs/api/reference/admin/roles/#patch-admin-roles-id)

```php
$event->on(RolesEvent::onRoleUpdated(), function ($role, $updatedData, $updatedBy): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:roles:role:updated ' . json_encode(['role' => $role, 'updated_data' => $updatedData, 'updated_by' => $updatedBy]));
});
```
