Event Hooks
In the previous Plugin Tutorial chapter, you might have noticed that our plugin logic was encompassed in two methods. Each of these methods onPluginsInitialized and onPageInitialized correspond to event hooks that are available throughout the Grav life cycle.
To fully harness the power of Grav plugins you need to know which event hooks are available, in what order are these hooks called, and what is available during these calls. The event hooks have a direct relationship to the overall Grav Lifecycle.
Event Order
Most events within Grav fire in a specific order and it is important to understand this order if you are creating plugins:
- onFatalException (no order, can occur anytime)
PluginsLoadedEventclass (1.7)PluginsLoadedEventclass (1.7)- onPluginsInitialized
FlexRegisterEventclass (1.7)- onThemeInitialized
- onRequestHandlerInit (1.6)
- onTask (1.6)
- onTask.{task}
- onAction (1.6)
- onAction.{action} (1.6)
- onBackupsInitialized
- onSchedulerInitialized (1.6)
- onAssetsInitialized
- onTwigTemplatePaths
- onTwigLoader
- onTwigInitialized
- onTwigExtensions
- onBuildPagesInitialized (once when pages are reprocessed)
- onPageProcessed (each page not cached yet)
- onFormPageHeaderProcessed (1.6) (each page not cached yet)
- onFolderProcessed (for each folder found)
- onPagesInitialized
- onPageInitialized
- onPageContentRaw (each page not cached yet)
- onMarkdownInitialized
- onPageContentProcessed (each page not cached yet)
- onPageContent (called first time Page::content() is called even when cached)
- onPageNotFound
- onPageAction (1.6)
- onPageAction.{action} (1.6)
- onPageTask (1.6)
- onPageTask.{task} (1.6)
- onTwigPageVariables (each page not cached yet)
- onHttpPostFilter (1.5.2)
- onTwigSiteVariables
- onCollectionProcessed (when collection is requested)
- onOutputGenerated
- onPageHeaders
- onOutputRendered
- onShutdown
Misc events:
- onBlueprintCreated
- onTwigTemplateVariables
- onTwigStringVariables
- onBeforeDownload
- onPageFallBackUrl
- onMediaLocate
- onGetPageBlueprints
- onGetPageTemplates
- onFlexObjectRender (1.6)
- onFlexCollectionRender (1.6)
- onBeforeCacheClear
- onImageMediumSaved (ImageFile)
- onAfterCacheClear (1.7)
- onHttpPostFilter (1.7)
PermissionsRegisterEventclass (1.7)
Core Grav Event Hooks
There are several core Grav event hooks that are triggered during the processing of a page:
onFatalException
This is an event that can be fired at any time if PHP throws a fatal exception. This is currently used by the problems plugin to handle displaying a list of potential reasons why Grav throws the fatal exception.
onPluginsInitialized
This is the first plugin event available. At this point the following objects have been initiated:
- Uri
- Config
- Debugger
- Cache
- Plugins
Tip
A plugin will not be loaded at all if the enabled: false configuration option has been set for that particular plugin.
onAssetsInitialized
The event indicates the assets manager has been initialized and is ready for assets to be added and managed.
onPagesInitialized
This event signifies that all the pages in Grav's user/pages folder have been loaded as objects and are available in the Pages object.
onPageNotFound
This is an event that can be fired if an expected page is not found. This is currently used by the error plugin to display a pretty 404 error page.
onPageInitialized
The current page as requested by a URL has been loaded into the Page object.
onOutputGenerated
The output has been processed by the Twig templating engine and is now just a string of HTML.
onPageHeaders
Allows the manipulation of the page headers object.
onOutputRendered
The output has been fully processed and sent to the display.
onShutdown
A new and very powerful event that lets you perform actions after Grav has finished processing and the connection to the client has been closed. This is particularly useful for performing actions that don't need user interaction and potentially could impact performance. Possible uses include user tracking and jobs processing.
onBeforeDownload
This new event passes in an event object that contains a file. This event can be used to perform logging, or grant/deny access to download said file.
onGetPageTemplates
This event enables plugins to provide their own templates in addition to the ones gathered from the theme's directory structure and core. This is especially useful if you wish the plugin to provide its own template.
Example
1/**
2 * Add page template types.
3 */
4public function onGetPageTemplates(Event $event)
5{
6 /** @var Types $types */
7 $types = $event->types;
8 $types->register('downloads');
9}
This allows a plugin to register a template (that it might provide) so that it shows up in the dropdown list of page template types (like when editing a page). In the example above, a template type of downloads is added as there is a downloads.html.twig file in the downloads directory.

onGetPageBlueprints
This event, like onGetPageTemplates enables the plugin to provide its own resources in addition to core and theme-specific ones. In this case, it's blueprints.
Example
1$scanBlueprintsAndTemplates = function () use ($grav) {
2 // Scan blueprints
3 $event = new Event();
4 $event->types = self::$types;
5 $grav->fireEvent('onGetPageBlueprints', $event);
6
7 self::$types->scanBlueprints('theme://blueprints/');
8
9 // Scan templates
10 $event = new Event();
11 $event->types = self::$types;
12 $grav->fireEvent('onGetPageTemplates', $event);
13
14 self::$types->scanTemplates('theme://templates/');
15};
In this example, we are using both the onGetPageTemplates and onGetPageBlueprints hooks to make these plugin-provided resources (templates and blueprints) available to Grav for inheritance and other uses.
Twig Event Hooks
Twig has its own set of event hooks.
onTwigTemplatePaths
The base locations for template paths have been set on the Twig object. If you need to add other locations where Twig will search for template paths, this is the event to use.
Example
1/**
2 * Add template directory to twig lookup path.
3 */
4 public function onTwigTemplatePaths()
5 {
6 $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
7 }
onTwigInitialized
The Twig templating engine is now initialized at this point.
onTwigExtensions
The core Twig extensions have been loaded, but if you need to add your own Twig extension, you can do so with this event hook.
onTwigPageVariables
Where Twig processes a page directly, i.e. when you set process: twig: true in a page's YAML headers. This is where you should add any variables to Twig that need to be available to Twig during this process.
onTwigSiteVariables
Where Twig processes the full site template hierarchy. This is where you should add any variables to Twig that need to be available to Twig during this process.
Collection Event Hooks
onCollectionProcessed
If you need to manipulate a collection after it has been processed this is the time to do it.
Page Event Hooks
onBuildPagesInitialized
This event is triggered once when pages are going to be reprocessed. This typically happens if the cache has expired or needs refreshing. This is a useful event to use for plugins that need to manipulate content and cache the results.
onBlueprintCreated
This is used for processing and handling forms.
onPageContentRaw
After a page has been found, header processed, but content not processed. This is fired for every page in the Grav system. Performance is not a problem because this event will not run on a cached page, only when the cache is cleared or a cache-clearing event occurs.
onPageProcessed
After a page is parsed and processed. This is fired for every page in the Grav system. Performance is not a problem because this event will not run on a cached page, only when the cache is cleared or a cache-clearing event occurs.
onMarkdownInitialized
Called when Markdown has been initialized. Allows to override the default Parsedown processing implementation. See an usage example on the PR that introduced it.
onPageContentProcessed
This event is fired after the page's content() method has processed the page content. This is particularly useful if you want to perform actions on the post-processed content but ensure the results are cached. Performance is not a problem because this event will not run on a cached page, only when the cache is cleared or a cache-clearing event occurs.
onFolderProcessed
After a folder is parsed and processed. This is fired for every folder in the Grav system. Performance is not a problem because this event will not run on a cached page, only when the cache is cleared or a cache-clearing event occurs.
onPageFallBackUrl
If a route is not recognized as a page, Grav tries to access a page media asset. The event is fired as soon as the procedure begins, so plugins can hook and provide additional functionality.
onMediaLocate
Adds support for custom media locations for excerpts.
onTwigLoader
Adds support for use of namespaces in conjunction with two new methods in Twig class: Twig::addPath($path, $namespace) and Twig::prependPath($path, $namespace).