Warning
You're browsing the documentation for an old version of Webiny. Consider upgrading your project to Webiny 5.39.x.
What you'll learn
  • what are lifecycle events
  • how lifecycle events work
  • how to subscribe to a lifecycle event

Lifecycle events using publish/subscribe pattern replace the hook plugins starting from version 5.18.0.

Overview
anchor

In our Headless CMS we provide lifecycle events available for you to hook into.

With the lifecycle events you can hook into a number of different operations.

For example, when using DynamoDB + Elasticsearchexternal link as storage layer, we use the onBeforeSystemInstall to insert the template for Elasticsearch index.

System
anchor

onBeforeSystemInstall
anchor

This event is triggered before the installation of the Headless CMS and insertion of initial “Ungrouped” group.

Event Arguments
anchor

PropertyDescription
tenantID of the current tenant

How to Subscribe to This Event?
anchor

onAfterSystemInstall
anchor

This event is triggered after the installation of the Headless CMS.

Event Arguments
anchor

PropertyDescription
tenantID of the current tenant

How to Subscribe to This Event?
anchor

Please note that in between onBeforeInstall and onAfterInstall we create a default CmsGroup named “Ungrouped” and, because of that, there are events onBeforeGroupCreate and onAfterGroupCreate being run.

Groups
anchor

onBeforeGroupCreate
anchor

This event is triggered before new group is stored into the database.

Event Arguments
anchor

PropertyDescription
groupGroup object which is going to be stored

How to Subscribe to This Event?
anchor

onAfterGroupCreate
anchor

This event is triggered after new group is stored into the database.

Event Arguments
anchor

PropertyDescription
groupGroup object which was stored

How to Subscribe to This Event?
anchor

onBeforeGroupUpdate
anchor

This event is triggered before group is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
originalGroup object which is received from the database
groupGroup object which is going to be stored

How to Subscribe to This Event?
anchor

onAfterGroupUpdate
anchor

This event is triggered before group is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
originalGroup object which is received from the database
groupGroup object which was stored

How to Subscribe to This Event?
anchor

onBeforeGroupDelete
anchor

This event is triggered before group is deleted from the database.

Event Arguments
anchor

PropertyDescription
groupGroup object which is going to be deleted

How to Subscribe to This Event?
anchor

onAfterGroupDelete
anchor

This event is triggered after group is deleted from the database.

Event Arguments
anchor

PropertyDescription
groupGroup object which was deleted

How to Subscribe to This Event?
anchor

Model
anchor

onBeforeModelCreate
anchor

This event is triggered before new model is stored into the database.

Event Arguments
anchor

PropertyDescription
modelModel object which is going to be stored

How to Subscribe to This Event?
anchor

onAfterModelCreate
anchor

This event is triggered before new model is stored into the database.

Event Arguments
anchor

PropertyDescription
modelModel object which is going to be stored

How to Subscribe to This Event?
anchor

onBeforeModelCreateFrom
anchor

This event is triggered before newly cloned model is stored into the database.

Event Arguments
anchor

PropertyDescription
originalModel object from which we are creating a new model
modelModel object which is going to be stored

How to Subscribe to This Event?
anchor

onAfterModelCreateFrom
anchor

This event is triggered after newly cloned model is stored into the database.

Event Arguments
anchor

PropertyDescription
originalModel object from which we are creating a new model
modelModel object which was stored

How to Subscribe to This Event?
anchor

onBeforeModelUpdate
anchor

This event is triggered before model is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
originalModel object from which we received from the database
modelModel object which is going to be stored

How to Subscribe to This Event?
anchor

onAfterModelUpdate
anchor

This event is triggered after model is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
originalModel object from which we received from the database
modelModel object which was stored

How to Subscribe to This Event?
anchor

onBeforeModelDelete
anchor

This event is triggered before model is deleted from the database.

Event Arguments
anchor

PropertyDescription
modelModel object which is going to be deleted

How to Subscribe to This Event?
anchor

onAfterModelDelete
anchor

This event is triggered after model is deleted from the database.

Event Arguments
anchor

PropertyDescription
modelModel object which was deleted

How to Subscribe to This Event?
anchor

Entry
anchor

Note that storageEntry, originalStorageEntry, latestStorageEntry and publishedStorageEntry are objects derived from the entry object which we want to store into the database. Those new objects have StorageTransformPlugin run on them to prepare them to be stored. To find out why we use StorageTransformPlugin, read about it in this article.

onBeforeEntryCreate
anchor

This event is triggered before entry is stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
entryEntry object which is going to be stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onAfterEntryCreate
anchor

This event is triggered after entry is stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
entryEntry object which was stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onBeforeEntryCreateRevision
anchor

This event is triggered before a new entry is created from originating entry and is stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
originalOriginal entry which we received from the database
entryEntry object which is going to be stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onAfterEntryCreateRevision
anchor

This event is triggered after a new entry is created from originating entry and is stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
originalOriginal entry which we received from the database
storageEntryEntry object prepared to be stored into the database
entryEntry object which was stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onBeforeEntryUpdate
anchor

This event is triggered before entry is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
originalOriginal entry which we received from the database
entryEntry object which is going to be stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onAfterEntryUpdate
anchor

This event is triggered after entry is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
originalOriginal entry which we received from the database
storageEntryEntry object prepared to be stored into the database
entryEntry object which was stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onBeforeEntryDelete
anchor

This event is triggered before all entry revisions are deleted from the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be deleted
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onAfterEntryDelete
anchor

This event is triggered after all entry revisions are deleted from the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be deleted
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onBeforeEntryDeleteRevision
anchor

This event is triggered before entry revision is deleted from the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be deleted
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onAfterEntryDeleteRevision
anchor

This event is triggered after entry revision is deleted from the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be deleted
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onBeforeEntryPublish
anchor

This event is triggered before entry is published.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be published
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onAfterEntryPublish
anchor

This event is triggered after entry is published.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be published
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onBeforeEntryUnpublish
anchor

This event is triggered before entry is unpublished.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be unpublished
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onAfterEntryUnpublish
anchor

This event is triggered after entry is unpublished.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be unpublished
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onBeforeEntryRequestChanges
anchor

This event is triggered before request changes entry object is stored to the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be modified and stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onAfterEntryRequestChanges
anchor

This event is triggered after request changes entry object is stored to the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be modified and stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onBeforeEntryRequestReview
anchor

This event is triggered before request review entry object is stored to the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be modified and stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onAfterEntryRequestReview
anchor

This event is triggered after request review entry object is stored to the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be modified and stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

Please, be aware that you can change what ever you want on the object before it is stored into the database, so be careful with changing the data.

Registering Lifecycle Event Subscriptions
anchor

System Lifecycle Events
anchor

For the subscriptions (your code) to be run, you must register it in the createHandler in the api/code/graphql/src/index.ts file.

api/code/graphql/src/index.ts

Group, Model and Entries Lifecycle Events
anchor

For the subscriptions (your code) to be run, you must register it in the createHandler in the api/code/headlessCMS/src/index.ts file.

api/code/headlessCMS/src/index.ts

Please, be aware that the order of subscribing matters, so if you want some event subscription to be executed before some other one, add it first.

The check condition for context.cms will not be required starting with version 5.22.0.