Hooks and events
Run your own code when an invoice is created, a client changes or the automation ticks, from an add-on module.
How events work#
An add-on module lists the events it listens to in its events() method, as event name => method name. CoreInto calls that method with one array of data when the event happens, only while the module is active. A module that throws does not stop CoreInto: the error is logged and the work carries on.
namespace Acme\Mod\Slack;
use CoreInto\Addons\Addon;
final class Module extends Addon
{
public function events(): array
{
return [
'invoice.created' => 'onInvoice',
'client.created' => 'onClient',
'tick' => 'onTick',
];
}
public function onInvoice(array $data): void
{
// $data: invoice_id, gateway, draft
}
public function onClient(array $data): void
{
// $data: client_id, opt_in
}
public function onTick(array $data): void
{
// every few minutes, from the automation: send what is queued
}
}The events#
| Event | When | Data |
|---|---|---|
invoice.created | An invoice is made: for an order, by the renewal run, from billable items, a quote, a debit note or by hand | invoice_id, gateway, draft |
invoice.gateway | An invoice's payment method is chosen or changed | invoice_id, gateway, previous |
invoice.changed | An invoice's amounts change, for example when its currency is changed | invoice_id, reason |
client.created | A client account is made, by sign-up, order, staff or the API | client_id, opt_in |
client.updated | A client's profile changes | client_id, changed (the fields), old_email |
client.marketing | A client opts in to or out of marketing email | client_id, opt_in |
tick | Every few minutes, from the automation | none |
Use tick for slow work, such as calling another service, instead of doing it while a page is being shown: queue it in the event, send it in tick.
The page head#
head(string $area, array $page): string returns HTML put at the end of the page head of the client area and store ('client') or the website ('site'). $page has path, client_id (0 when nobody is signed in) and events, such as ['purchase' => [...]] on the page after an order, with the order number, value, currency and items. The Google Analytics module uses this to report purchases. Scripts must follow the page security policy: load them from your module's assets or a host the policy allows.
Other hooks of an add-on module#
| Method | What it does |
|---|---|
activate() / deactivate() | Create your tables with CREATE TABLE IF NOT EXISTS; keep data on deactivate |
adminRoutes() | Pages under /admin, each with a permission |
menu() | Items in the Add-ons menu |
configRoute() | Your settings page |
registrars() | Registrar modules the add-on brings |
whois() and whoisTlds() | WHOIS answers for extensions you handle |
conf() and saveConf() | Read and save your settings |
The add-on modules page has the folder layout and the manifest.
Frequently asked questions
Does a failing hook break CoreInto?
No. The error is logged and CoreInto carries on.
Where should slow work go?
In the tick event, which runs every few minutes from the automation, not in an event that fires while a page is shown.