Skip to content

pbOnBeforeSave

Triggers before saving a block or table

Parameters

ParameterDescription
modeSave mode: new|upd
typeType: pbBlock|pbTable
id0
objectnull
valuesArray of values
savePermission to save
messageCustom 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.';
        }
    }
}

© PageBlocks 2019-present