GRAV CMS DEVELOPER

WhatsApp/Signal: +91-953-66666-77
Email ID: hello@gravdeveloper.com
Grav CMS Event Hooks and Admin Event Hooks

Grav CMS Event Hooks and Admin Event Hooks

  • Date: May 26, 2025
  • Tags: GRAV CMS

Grav CMS is a modern, flat-file content management system that offers a robust event-driven architecture. This architecture allows developers to extend and customize the CMS's functionality through event hooks. These hooks enable plugins and themes to interact with Grav's core processes and the Admin Panel, providing a flexible and powerful development environment.

Core Event Hooks in Grav CMS

Grav's core event hooks are triggered at various stages of the CMS's lifecycle, allowing developers to inject custom functionality. Some of the key core event hooks include:

onPluginsInitialized: This is the first event triggered during Grav's initialization process. It allows plugins to perform setup tasks, such as initializing services or registering other event listeners.

onPagesInitialized: Fired after all pages have been loaded and are available in the Pages object. This hook is useful for manipulating page data before rendering.

onPageInitialized: Triggered when the current page is fully initialized. Developers can use this hook to modify page content or metadata.

onTwigTemplatePaths: Allows plugins to add their own template paths to Twig's loader, enabling the use of custom templates.

onTwigSiteVariables: Provides an opportunity to add variables to Twig's global scope, making them accessible in templates.

onOutputGenerated: Fired after the page content has been processed by Twig but before it's sent to the browser. This hook can be used to modify the final output.

onShutdown: Triggered after Grav has finished processing and the response has been sent to the client. Useful for tasks that don't require user interaction, such as logging or cleanup operations.

These hooks provide granular control over Grav's behavior, enabling developers to create highly customized functionality.

Admin Event Hooks in Grav CMS

The Grav Admin Panel also exposes its own set of event hooks, allowing developers to interact with administrative actions. Key admin event hooks include:

onAdminTaskExecute: Triggered when an admin task is executed, such as saving a page or clearing the cache.

onAdminCreatePageFrontmatter: Fired during the creation of a new page, allowing plugins to modify the default frontmatter.

onAdminSave: Occurs before a page is saved, providing an opportunity to alter the page object.

onAdminAfterSave: Triggered after a page has been saved, useful for post-save operations.

onAdminAfterDelete: Fired after a page or folder is deleted, allowing for cleanup tasks.

onAdminAfterAddMedia and onAdminAfterDelMedia: Triggered after media files are added or deleted, respectively.

These admin hooks are essential for extending the functionality of the Admin Panel and integrating custom features.

Working with Event Hooks

To utilize these event hooks, developers create plugins that subscribe to specific events. A plugin defines its subscribed events and corresponding handler methods. For example:

php ----------------------

  • public static function getSubscribedEvents()
  • {
  • return [
  • 'onPluginsInitialized' => ['onPluginsInitialized', 0],
  • 'onAdminSave' => ['onAdminSave', 0],
  • ];
  • }
  • public function onPluginsInitialized()
  • {
  • // Initialization code here
  • }
  • public function onAdminSave(Event $event)
  • {
  • // Code to execute before a page is saved
  • }
  • By subscribing to these events, plugins can seamlessly integrate with Grav's core and admin functionalities.

Conclusion

Grav CMS's event-driven architecture provides a powerful mechanism for extending and customizing both the frontend and backend functionalities. By leveraging core and admin event hooks, developers can create plugins that interact deeply with the CMS's lifecycle, offering tailored solutions to meet specific requirements.

For a comprehensive list of available event hooks and detailed documentation, refer to Grav's official resources:

Core Event Hooks

Admin Event Hooks

By mastering these hooks, you can unlock the full potential of Grav CMS and build sophisticated, customized web applications.

Feel free to reach out if you'd like to make changes to your Grav website or simply chat about your Grav project. I'm available for a free consultation!