You can execute a function when a specific event occurs within Static CMSD.
Supported events are:
Name 17468_1807b7-e1> |
Description 17468_152265-4e> |
---|---|
17468_ef1098-08> |
Event fires once Static CMS is fully loaded 17468_1ca379-8f> |
17468_aa397e-c5> |
Event fires when a user logs into Static CMS 17468_daa761-fd> |
17468_3a9798-80> |
Event fires when a user logs out of Static CMS 17468_d0b9ab-79> |
17468_d6b7b3-6e> |
Event fires when a user changes the value of a field in the editor 17468_f3dcc6-96> |
17468_9700c8-79> |
Event fires before the changes have been saved to your git backend 17468_b34192-9b> |
17468_3d111d-60> |
Event fires after the changes have been saved to your git backend 17468_ac8e26-b0> |
17468_6a308f-44> |
Editorial Workflow ONLY. Event fires before the entry is “published”, before the PR is merged into default branch 17468_31a68c-e7> |
17468_4f9926-0c> |
Editorial Workflow ONLY. Event fires after the entry is “published”, after the PR is merged into default branch 17468_f9e70b-15> |
Mounted Event
The mounted
event handler fires once Static CMS is fully loaded.
CMS.registerEventListener({
name: 'mounted',
handler: () => {
// your code here
},
});
Login Event
The login
event handler fires when a user logs into Static CMS.
CMS.registerEventListener({
name: 'login',
handler: ({ author: { login, name } }) => {
// your code here
},
});
Logout Event
The logout
event handler fires when a user logs out of Static CMS.
CMS.registerEventListener({
name: 'logout',
handler: () => {
// your code here
},
});
Change Event
The change
event handler fires when a user changes the value of a field in the editor. Event listeners for change
can optionally provide collection, file and field names. They can also be used to modify the entry data.
// Listen for ALL change events
CMS.registerEventListener({
name: 'change',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all change events in a specific collection
CMS.registerEventListener({
name: 'change',
collection: 'posts',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all change events in a specific file in a collection
CMS.registerEventListener({
name: 'change',
collection: 'settings',
file: 'global',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all change events in a specific field in a collection
CMS.registerEventListener({
name: 'change',
collection: 'posts',
// file: 'global', // You can specify a file if in a file collection
field: 'path.to.my.field',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Alter the entry data when a specific field changes
CMS.registerEventListener({
name: 'change',
collection: 'posts',
// file: 'global', // You can specify a file if in a file collection
field: 'path.to.my.field',
handler: ({ data, collection, field }) => {
const currentValue = data.path.to.my.field;
return {
...data,
path: {
...data.path,
to: {
...data.path.to,
my: {
...data.path.to.my,
field: `new${currentValue}`,
},
},
},
};
},
});
Pre Save Event
The preSave
event handler fires before the changes have been saved to your git backend, and can be used to modify the entry data.
// Listen for ALL preSave events
CMS.registerEventListener({
name: 'preSave',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all preSave events in a specific collection
CMS.registerEventListener({
name: 'preSave',
collection: 'posts',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all preSave events in a specific file in a collection
CMS.registerEventListener({
name: 'preSave',
collection: 'settings',
file: 'global',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Alter the entry data
CMS.registerEventListener({
name: 'preSave',
collection: 'posts',
// file: 'global', // You can specify a file if in a file collection
handler: ({ data, collection, field }) => {
const currentValue = data.path.to.my.field;
return {
...data,
path: {
...data.path,
to: {
...data.path.to,
my: {
...data.path.to.my,
field: `new${currentValue}`,
},
},
},
};
},
});
Post Save Event
The postSave
event handler fires after the changes have been saved to your git backend.
// Listen for ALL postSave events
CMS.registerEventListener({
name: 'postSave',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all postSave events in a specific collection
CMS.registerEventListener({
name: 'postSave',
collection: 'posts',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all postSave events in a specific file in a collection
CMS.registerEventListener({
name: 'postSave',
collection: 'settings',
file: 'global',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
Pre Publish Event
Editorial Workflow ONLY
The prePublish
event handler fires before the entry is “published”, before the PR is merged into default branch.
// Listen for ALL prePublish events
CMS.registerEventListener({
name: 'prePublish',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all prePublish events in a specific collection
CMS.registerEventListener({
name: 'prePublish',
collection: 'posts',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all prePublish events in a specific file in a collection
CMS.registerEventListener({
name: 'prePublish',
collection: 'settings',
file: 'global',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
Post Publish Event
Editorial Workflow ONLY
The postPublish
event handler fires after the entry is “published”, after the PR is merged into default branch.
// Listen for ALL postPublish events
CMS.registerEventListener({
name: 'postPublish',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all postPublish events in a specific collection
CMS.registerEventListener({
name: 'postPublish',
collection: 'posts',
handler: ({ data, collection, field }) => {
// Your handler code
},
});
// Listen for all postPublish events in a specific file in a collection
CMS.registerEventListener({
name: 'postPublish',
collection: 'settings',
file: 'global',
handler: ({ data, collection, field }) => {
// Your handler code
},
});