pbOnBeforeSave
Triggers before saving a block or table
Parameters
| Parameter | Description |
|---|---|
| mode | Save mode: new|upd |
| type | Type: pbBlock|pbTable |
| id | 0 |
| object | null |
| values | Array of values |
| save | Permission to save |
| message | Custom message |
Example
Do not allow saving a block with a duplicate title.
php
if ($modx->event->name === 'pbOnBeforeSave') {
if ($type === 'pbBlock' && $mode === 'new' && $model_id === 234 && $constructor_id === 93) {
$title = $values['title'];
$dublicate = $modx->getCount(pbBlockValue::class, [
'model_type' => $model_type,
'model_id' => $model_id,
'constructor_id' => $constructor_id,
'values:LIKE' => '%"title":"'.$title.'"%'
]);
if ($dublicate) {
$modx->event->params['save'] = false;
$modx->event->params['message'] = 'A block with this title already exists!';
}
}
}Do not save the table if no benefit is added.
php
if ($modx->event->name === 'pbOnBeforeSave') {
if ($type === 'pbTable' && $mode === 'new' && $model_id === 234 && $constructor_id === 51) {
if (!isset($values['items']) || count($values['items']) < 1) {
$modx->event->params['save'] = false;
$modx->event->params['message'] = 'You must add at least one benefit.';
}
}
}