System Events
For the most part, the events' naming convention makes it pretty clear at what point they are invoked. However, the events used in the parser itself are worth taking a closer look at.
- OnWebPageInit - This is the first event to be invoked when generating a page. The parser has used the HTML string (alias or id) to determine which document has been requested. The document's ID is loaded into the documentIdentifier variable. These arrays and variables are available:
- $modx->config[]
- $modx->documentIdentifier
- OnLogPageHit - as soon as it's determined which document this is, immediately after the onWebPageInit event, a check is made whether or not the document's 'track_visitors' field is set to 1; if it is, this event is made available.
- OnPageNotFound - Actually, this event is invoked in more than one situation. Of course one would expect it to be invoked if the document requested is not found, and it is. But it is also invoked if the page is unpublished and the user is not logged in as a Manager user, or if the Manager user does not have permissions to view unpublished documents. It is also invoked if the document is tagged for deletion. Since it is invoked immediately before redirecting the visitor to the "Not Found" error page, it would be a good place to customize your 404 pages. Available arrays and variables:
- $modx->config[]
- $modx->documentIdentifier
- OnLoadWebPageCache - this event is called if the page is being loaded from cache rather than being generated. The document's properties were serialized and included in the cache file; this is now unserialized and loaded into the documentObject array. The content from the page's cache file is loaded into the documentContent. Any document groups the document belonged to are checked against the user's SESSION to make sure the user has permission to view the page. These arrays and variables are available:
- $modx->config[]
- $modx->documentIdentifier
- $modx->documentObject[]
- $modx->documentContent
- OnPageUnauthorized - This event is invoked if the user does not have permission to access the page, before the user is redirected to the site's "Unauthorized" page. The variables and arrays available depend on whether or not the document was cached.
If the document was not cached, the document's document groups are retrieved and the user's SESSION is checked to make sure he has permission to access the page. This is done before the documentObject is loaded from the database. These arrays and variables are available:
- $modx->config[]
- $modx->documentIdentifier
- $modx->config[]
- $modx->documentIdentifier
- $modx->documentObject[]
- $modx->documentContent
- OnLoadWebDocument - this event is invoked when the document's template (or its content, if it uses a blank template) is loaded, before any parsing is begun. At this point, these variables and arrays are available:
- $modx->config[]
- $modx->documentIdentifier
- $modx->documentObject[]
- $mdox->documentName
- $modx->documentContent
- OnParseDocument - After the OnLoadWebDocument event, the raw documentContent is passed to the ParseDocumentSource() function. The documentContent is loaded into an internal $source variable, which is in turn loaded into the documentOutput, then this event is invoked. So we now have the following variables and arrays available:
- $modx->config[]
- $modx->documentIdentifier
- $modx->documentName
- $modx->documentObject[]
- $modx->documentContent
- $modx->documentOutput
- OnBeforeWebSaveCache - this event is invoked immediately before the document is cached. The documentObject array has the document's assigned document groups (if any) added to it, then the array is serialized. This and the documentContent are written to the cache file for this document.
- OnWebPageComplete - The page content has been parsed (or loaded from cache), but there still may be more passes through the parser. Uncached snippets may still need to be processed, and chunks and TVs may have had MODx tags in them that need another pass through the parser. These variables and arrays are available:
- $mdox->config[]
- $modx->documentIdentifier
- $modx->documentName
- $modx->documentObject[]
- $modx->documentContent
- $modx->documentOutput
- onWebPagePrerender - this is the last event of the document parsing process, immediately before the finished document is sent to the server. This event is probably the one most commonly used; since the entire document is available in the documentOutput variable, additional parsing and modifications to the finished document can be added here. For example, the email obfuscation plugins use this event to modify all links that are mailto: links.
System events are registered with the system_eventnames table. A developer creating a module or snippet that uses events can add its events to the database, providing users with checkboxes along with the rest of the registered System Events so they can use your events in a plugin. An example of this would be the WebLogin snippet.
Comment On This Article
Do you find something unclear? Did I miss something or get something wrong? What do you like or not like about this chapter? Please do not ask for help here; use the forums if you need help.
Last Edit: June 02, 2010, 12:27:54 by guest