Skip to content

Events

All events have common parameters

ParameterDescription
model_typeModel type
model_idModel id
constructor_idConstructor id
parent_idResource parent
template_idResource template
context_keyResource context key

model_type - model type where a block or table is created.

typemodel_type
pbBlockmodDocument|ReadyBlock
pbTablemodDocument|ReadyTable|pbBlock|pbTable

pbOnBeforeCreate

Triggers before creating a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
createPermission to create: true|false
messageCustom message

Examples


We prohibit all users except the admin from creating new blocks.

php
if ($modx->event->name === 'pbOnBeforeCreate') {
    if ($modx->user->id > 1 && $type === 'pbBlock') {
        $modx->event->params['create'] = false;
    }
}

Adding a custom message

php
if ($modx->event->name === 'pbOnBeforeCreate') {
    if ($modx->user->id > 1 && $type === 'pbBlock') {
        $modx->event->params['create'] = false;
        $modx->event->params['message'] = "You're not worthy to create blocks!";
    }
}

We prohibit creating blocks and tables for resources with a template of 2.

php
if ($modx->event->name === 'pbOnBeforeCreate') {
    if ($template_id === 2) {
        $modx->event->params['create'] = false;
        $modx->event->params['message'] = "You're not worthy to create blocks!";
    }
}

pbOnAfterCreate

Triggers after creating a block or table. Getting the list of fields.

Parameters

ParameterDescription
typeType: pbBlock|pbTable
arrayArray of fields

Examples


Changing the field for the table

php
if ($modx->event->name === 'pbOnAfterCreate') {
    if ($type === 'pbTable' && $model_id === 234 && $constructor_id === 51) {
        $modx->event->params['array'] = array_map(function($field) {
            if ($field['name'] === 'title') {
                $field['type'] = 'displayfield';
                $field['required'] = false;
                $field['value'] = 'Custom title';
            }
            return $field;
        }, $array);
    }
}

Changing the field for the block

php
if ($modx->event->name === 'pbOnAfterCreate') {
    if ($type === 'pbBlock' && $model_id === 234 && $constructor_id === 93) {
        $modx->event->params['array'] = array_map(function($field) {
            if ($field['name'] === 'title') {
                $field['required'] = false;
                $field['default'] = 12;
            }
            return $field;
        }, $array);
    }
}

pbOnGetListBlocks

Triggers when retrieving the list of blocks.

Parameters

ParameterDescription
arrayArray of rows

Examples


php
if ($modx->event->name === 'pbOnGetListBlocks') {
    if ($parent_id === 12 && $type === 'pbBlock') {
        $modx->event->params['array'] = array_filter($array, function($block){
            return $block['name'] !== 'Gallery';
        });
    }
}

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.';
        }
    }
}

pbOnAfterSave

Triggers after saving a block or table

Parameters

ParameterDescription
modeSave mode: new|upd
typeType: pbBlock|pbTable
idObject id
objectObject
valuesArray of values

pbOnBeforePublished

Triggers before publishing a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
idObject id
objectObject
publishedPermission to published
messageCustom message

Examples


Allow publishing only for moderators.

php
if ($modx->event->name === 'pbOnBeforePublished') {
    if ($modx->user->id !== 2) {
        $modx->event->params['published'] = false;
        $modx->event->params['message'] = "You're not yet qualified to be a moderator.";
    }
}

pbOnAfterPublished

Triggers after publishing a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
idObject id
objectObject

pbOnBeforeUnPublished

Triggers before unpublishing a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
idObject id
objectObject
unpublishedPermission to unpublish
messageCustom message

Examples


Do not allow unpublishing blocks.

php
if ($modx->event->name === 'pbOnBeforeUnPublished') {
    if ($type === 'pbBlock') {
        $modx->event->params['unpublished'] = false;
        $modx->event->params['message'] = "You cannot unpublish";
    }
}

pbOnAfterUnPublished

Triggers after unpublishing a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
idObject id
objectObject

pbOnBeforeDuplicate

Triggers before copying a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
idObject id
objectObject
valuesArray of values
copyPermission to copy
messageCustom message

Examples


When copying, change the title.

php
if ($modx->event->name === 'pbOnBeforeDuplicate') {
    if ($type === 'pbBlock' && isset($values['title'])) {
        $values['title'] = $values['title'] . ' DUPLICATE';
        $modx->event->params['values'] = $values;
    }
}

pbOnAfterDuplicate

Triggers after copying a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
idObject id
objectObject
newObjectnewObject

pbOnBeforeDelete

Triggers before deleting a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
idObject id
objectObject
deletePermission to delete
messageCustom message

Examples


Do not allow deletion and send a message to the admin so they can punish the wrongdoer.

php
if ($modx->event->name === 'pbOnBeforeDelete') {
    if ($modx->user->id > 1) {
        $modx->event->params['delete'] = false;
        $modx->event->params['message'] = "You'll be punished soon";
        
        $admin = $modx->getObject(modUser::class, 1);
        if ($admin) {
            $body = sprintf('<p>User %s decided to break the rules</p>', $modx->user->username);
            $params = [
                '<li>Type of violation: Deletion</li>',
                sprintf('<li>Resource: %s</li>', $model_id),
                sprintf('<li>Type: %s</li>', $type),
                sprintf('<li>Object id: %s</li>', $id)
            ];
            $body .= sprintf('<ul>%s</ul>', implode('', $params));
            
            $admin->sendEmail($body, [
                'subject' => sprintf('Violation of rules | %s', $modx->getOption('site_name'))
            ]);
        }
    }
}

pbOnAfterDelete

Triggers after deleting a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
idObject id
objectObject

pbOnGetValues

Triggers when retrieving values from a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
idObject id: 0
objectObject
valuesArray of values

pbOnBeforeGetList

Triggers before generating the grid of a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
whereArray of conditions

Examples


Do not display unpublished objects.

php
if ($modx->event->name === 'pbOnBeforeGetList') {
    if ($modx->user->id > 1) {
        $where['published'] = 1;
        $modx->event->params['where'] = $where;
    }
}

pbOnAfterGetList

Triggers after generating the grid of a block or table

Parameters

ParameterDescription
typeType: pbBlock|pbTable
arrayArray of rows

Examples


Delete all actions.

php
if ($modx->event->name === 'pbOnAfterGetList') {
    if ($modx->user->id > 1) {
        $array = array_map(function($item) use ($modx){
            $item['actions'] = '';
            return $item;
        }, $array);
        $modx->event->params['array'] = $array;
    }
}

pbOnFieldValues

Triggers after values are received

Parameters

ParameterDescription
fieldField object
valuesArray of values

Examples


Adding custom values.

php
if ($modx->event->name === 'pbOnFieldValues') {
    if ($field->name === 'benefits') {
        $modx->event->params['values'] = [
            ['name' => 'Title', 'value' => 'Value'],
            ['name' => 'Title2', 'value' => 'Value2'],
            ['name' => 'Title3', 'value' => 'Value3'],
        ];
    }
}

© PageBlocks 2019-present