Events
All events have common parameters
Parameter | Description |
---|---|
model_type | Model type |
model_id | Model id |
constructor_id | Constructor id |
parent_id | Resource parent |
template_id | Resource template |
context_key | Resource context key |
model_type - model type where a block or table is created.
type | model_type |
---|---|
pbBlock | modDocument|ReadyBlock |
pbTable | modDocument|ReadyTable|pbBlock|pbTable |
pbOnBeforeCreate
Triggers before creating a block or table
Parameters
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
create | Permission to create: true|false |
message | Custom message |
Examples
We prohibit all users except the admin from creating new blocks.
if ($modx->event->name === 'pbOnBeforeCreate') {
if ($modx->user->id > 1 && $type === 'pbBlock') {
$modx->event->params['create'] = false;
}
}
Adding a custom message
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.
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
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
array | Array of fields |
Examples
Changing the field for the table
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
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
Parameter | Description |
---|---|
array | Array of rows |
Examples
We remove the Gallery block from the list of all blocks for resources with a parent of 12
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
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.
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.
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
Parameter | Description |
---|---|
mode | Save mode: new|upd |
type | Type: pbBlock|pbTable |
id | Object id |
object | Object |
values | Array of values |
pbOnBeforePublished
Triggers before publishing a block or table
Parameters
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
id | Object id |
object | Object |
published | Permission to published |
message | Custom message |
Examples
Allow publishing only for moderators.
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
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
id | Object id |
object | Object |
pbOnBeforeUnPublished
Triggers before unpublishing a block or table
Parameters
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
id | Object id |
object | Object |
unpublished | Permission to unpublish |
message | Custom message |
Examples
Do not allow unpublishing blocks.
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
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
id | Object id |
object | Object |
pbOnBeforeDuplicate
Triggers before copying a block or table
Parameters
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
id | Object id |
object | Object |
values | Array of values |
copy | Permission to copy |
message | Custom message |
Examples
When copying, change the title.
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
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
id | Object id |
object | Object |
newObject | newObject |
pbOnBeforeDelete
Triggers before deleting a block or table
Parameters
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
id | Object id |
object | Object |
delete | Permission to delete |
message | Custom message |
Examples
Do not allow deletion and send a message to the admin so they can punish the wrongdoer.
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
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
id | Object id |
object | Object |
pbOnGetValues
Triggers when retrieving values from a block or table
Parameters
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
id | Object id: 0 |
object | Object |
values | Array of values |
pbOnBeforeGetList
Triggers before generating the grid of a block or table
Parameters
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
where | Array of conditions |
Examples
Do not display unpublished objects.
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
Parameter | Description |
---|---|
type | Type: pbBlock|pbTable |
array | Array of rows |
Examples
Delete all actions.
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
Parameter | Description |
---|---|
field | Field object |
values | Array of values |
Examples
Adding custom values.
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'],
];
}
}