Logo

Using the Hook System

Concept

Hooks are the primary way for modules to interact with the core application without modifying its files. They act as "event listeners" at specific points in the code. When a hook point is reached, the system checks if any active modules have registered a function for that hook and executes it.

Practical Example: Adding a Button to a Table

Let's add a custom "View Details" button to the actions column of the main Transactions table.

Step 1: Register the Hook in `module.json`

First, you must tell the system that your module wants to listen for a specific hook. We will use the TRANSACTIONS_ACTIONS_COLUMN hook point.

File: private/modules/MyModule/module.json

{
    "name": "My First Module",
    "version": "1.0",
    "description": "A simple module that adds a new page.",
    "hooks": [
        {
            "hook_point": "TRANSACTIONS_ACTIONS_COLUMN",
            "file": "hooks/transaction_actions.php",
            "priority": 10
        }
    ]
}

Step 2: Create the Hook File

Create the file specified in the manifest. The system will automatically include this file when the hook is triggered.

File: private/modules/MyModule/hooks/transaction_actions.php

 100) {
    echo 'Details';
}
?>

Step 3: Understanding the `$context`

The $context variable is how the core application passes data to your hook. The data available depends on the specific hook point. For `..._ACTIONS_COLUMN` hooks, it will almost always contain an associative array of the data for that table row (e.g., `$context['transaction']`, `$context['account']`, etc.).