What are Hooks?
avatar
Señor FAQ

¡Hola, amigos! I’m Señor FAQ, the mustached maestro of questions and answers! With my trusty glasses and a book of endless wisdom, I turn dudas into solutions. Soy el héroe de los curiosos and the champion of clarity.


Understanding Hooks in the JScms

Hooks are a powerful feature of the JScms, allowing developers to extend and modify its functionality without altering the core code. They provide predefined points in the system where custom logic or features can be injected dynamically.

What Are Hooks?

Hooks are essentially placeholders in the JScms code where custom functions, known as callbacks, can be executed. They enable flexibility and modularity by allowing plugins and extensions to interact with the core system seamlessly.

How Hooks Work

  1. Registration: Developers register their callbacks to specific hooks using the DynamicHooks::addHook() method.
  2. Execution: When the JScms reaches a hook point, all registered callbacks for that hook are executed in priority order.
  3. Result: Callbacks can return values, modify data, or perform other actions as needed.

Key Features of the Hook System

  • Namespaces: Each callback is associated with a unique namespace to prevent conflicts.
  • Priorities: Callbacks are executed based on their priority, with lower numbers running first.
  • Error Handling: Errors in callbacks are logged without interrupting the execution of other callbacks.
  • Flexibility: Hooks support passing multiple parameters to callbacks and even allow merging results when needed.

Registering a Hook

To register a callback to a hook, use the following method:

DynamicHooks::addHook('hook_name', function($param1, $param2) {
    // Custom logic here
}, 'unique_namespace', 10);
        

Parameters:

  • hook_name: The name of the hook to attach the callback to.
  • callback: The function to execute when the hook is triggered.
  • unique_namespace: A unique identifier for the callback to avoid conflicts.
  • priority: (Optional) The execution priority of the callback (default is 10).

Executing Hooks

To execute a hook, use the following method:

DynamicHooks::executeHook('hook_name', false, $param1, $param2);
        

This method executes all callbacks registered to the specified hook. Additional parameters can be passed to the callbacks.

Best Practices

  • Use meaningful names for hooks and namespaces to improve readability and avoid conflicts.
  • Ensure callbacks are lightweight to maintain performance.
  • Test thoroughly to avoid unexpected side effects.

Common Hooks

The JScms comes with several predefined hooks for various stages of execution. For example:

  • before_render - Executed before the template and editor are loaded.
  • after_save - Triggered after saving content.
  • user_login - Fired when a user logs in.

Find all hooks described in details in the FAQ hooks category

Need Help?

If you're unsure how to use hooks or encounter issues, refer to the documentation or contact support.

Was this article helpful?
0 out of 0 found this helpful