Appearance
Code hooks
Code hooks are PHP code written in Administration › Code hooks, for what fields, formulas and rules cannot do: a value worked out by a program, a form that fills itself in as it is typed in, an address other programs can call, and, with Qorekit Pro, a step of an automation rule. Only super admins write them.
Code hooks run with Qorekit's own rights
The code runs on your server and can read and change every record. That is why code hooks are off until the server's owner turns them on, why your password is asked again for every change, and why every change is kept in the audit log, code and all.
Turning them on
Add this line to .env, with your panel's file manager:
ini
QOREKIT_CODE_HOOKS=trueThe hooks are kept as files in the plugins/local/hooks folder, which updates of Qorekit never touch. Turned off again, hooks do not run, and code fields keep the values they have.
Writing a hook
Click New hook, give it a name and a key, and choose its kind. Write only the inside of a function: the code gets $hook and returns its answer.
php
return $hook->value('first_name') . ' ' . $hook->value('last_name');The code is checked when it is saved: a line with a mistake is named. Then type your password and save.
Value hooks
A value hook works out the value of a Code field when its record is saved. Add a field of the type Code to an entity, choose the hook and what it gives (a number, text, a date, a date and time, or yes or no); the value is stored, so lists sort and filter by it. The hook sees the record's other values, formulas included.
If the code fails, the field is left empty and the error is written to the log.
Change hooks
A change hook runs while a form of its entity is filled in, each time one of the fields it follows changes. It gets the form's values and returns the values to fill in, by field key:
php
// Fill in the city's region as the city is chosen.
$regions = ['Austin' => 'South', 'Denver' => 'West'];
return ['region' => $regions[$hook->value('city')] ?? null];Endpoints
An endpoint answers at its own address, /api/v1/hooks/{key}, to anyone signed in (in the browser, or with the REST API). It gets the request's query and JSON body as $hook->input(), and its answer is sent back as JSON.
php
$open = $hook->records()->list('deals', ['stage' => 'open']);
return ['open' => count($open)];Automation steps (Qorekit Pro)
With Qorekit Pro, a hook of the kind Automation step runs as the step Run code of an automation rule of its entity. It gets the record's values, and returns the fields to set on the record, by field key; other keys are left out. $hook->input() holds the record's id (record_id), what the rule's button form got (form) and, for a rule a request sets off, what the request brought (request). $hook->user() is whoever set the rule off, and the records the hook writes name the rule in their history.
php
// Work out the total once someone presses "Price it".
$total = $hook->value('quantity') * $hook->value('unit_price');
return ['total' => $total, 'priced_by' => $hook->user()['id'] ?? null];If the code fails, the step fails with its message, and the run is tried again as any failed step is. Only super admins put Run code in a rule, and a rule that runs code is theirs alone to change, turn off or delete.
What $hook offers
$hook->values(), $hook->value('key') | The record's or the form's values, as the REST API reads them |
$hook->changed() | For a change hook: the field that changed |
$hook->input() | For an endpoint: the request's query and body. For an automation step: the record's id, the button's form and the request |
$hook->variable('key') | A variable's value |
$hook->records() | Records of any entity: find, list, create, update. Saves go through Qorekit as any other, with their checks, formulas and history |
$hook->user() | Who is acting: id, name and email; nothing for a value hook run on its own |
$hook->http() | Requests to other sites (Laravel's HTTP client), with a time limit; addresses of the server's own network are refused |
$hook->log('…') | A line in the application's log |
Records that a hook reads and writes are not limited by roles: the hook acts for the installation.