Modules & Hook System
Relevant source files
The following files were used as context for generating this wiki page:
- src/PrestaShopBundle/Command/GenerateHooksDocumentationCommand.php
- tests/Integration/Behaviour/Features/Context/Domain/ModuleFeatureContext.php
- tests/UI/campaigns/modules/07_ps_apiresources/01_installation/01_uninstallAndInstallModule.ts
- tests/UI/campaigns/modules/02_blockwishlist/01_installation/05_uninstallAndDeleteModule.ts
- classes/Tools.php
- README.md
PrestaShop utilizes a modular architecture and a hook-based event system to extend core functionality. Modules are independent packages that add specific features to the front or back office, while hooks serve as insertion points where these modules can execute code or display content. Sources: README.md, src/PrestaShopBundle/Command/GenerateHooksDocumentationCommand.php
Module Lifecycle Management
Modules undergo several states during their lifecycle, managed through commands and the Back Office interface. The system tracks technical names, versions, and installation status. Sources: tests/Integration/Behaviour/Features/Context/Domain/ModuleFeatureContext.php:198-212
Key Module Actions
The ModuleFeatureContext class interacts with the command bus to execute the following operations:
- Installation: Registers the module in the database and prepares its environment. Sources: tests/Integration/Behaviour/Features/Context/Domain/ModuleFeatureContext.php:152-160
- Uninstallation: Removes the module and optionally deletes its physical files. Sources: tests/Integration/Behaviour/Features/Context/Domain/ModuleFeatureContext.php:97-106
- Enable/Disable: Toggles module activity without removing data. Sources: tests/Integration/Behaviour/Features/Context/Domain/ModuleFeatureContext.php:82-95
- Upgrade: Updates a module to a newer version when files are replaced. Sources: tests/Integration/Behaviour/Features/Context/Domain/ModuleFeatureContext.php:186-194
- Reset: Reinitializes a module to its default installed state. Sources: tests/Integration/Behaviour/Features/Context/Domain/ModuleFeatureContext.php:137-150
Module Installation Flow
The following diagram illustrates the sequence for installing a module and verifying its impact on system resources like API scopes.
Hook System Architecture
Hooks are categorized primarily into two types: Action hooks and Display hooks. The system uses a HookExtractor to identify these points within the codebase for documentation and execution purposes. Sources: src/PrestaShopBundle/Command/GenerateHooksDocumentationCommand.php:267-277
Hook Types
| Type | Description | Naming Convention |
|---|---|---|
| Action | Triggered when a specific event occurs (e.g., updating a product). Used for data processing. | Starts with action |
| Display | Triggered when rendering a view. Used to inject HTML content into templates. | Starts with display |
| Dynamic | Generated at runtime, often for Grid components (Definitions, QueryBuilders, Data). | Contains variable segments |
Sources: src/PrestaShopBundle/Command/GenerateHooksDocumentationCommand.php:27-60, 267-277
Dynamic Grid Hooks
Grids in the modern Back Office automatically dispatch specific hooks based on their unique ID. This allows modules to modify grid definitions, filter forms, or data presentation without core changes. Sources: src/PrestaShopBundle/Command/GenerateHooksDocumentationCommand.php:27-60
Implementation Details
Hook Dispatching
Hooks are dispatched using a HookDispatcher. For dynamic hooks, the dispatcher uses dispatchWithParameters to pass contextual objects (like a search_query_builder or presented_grid) to subscribers. Sources: src/PrestaShopBundle/Command/GenerateHooksDocumentationCommand.php:30-58
Module UI Testing
Automated UI tests verify the module lifecycle by interacting with the Back Office (BO) and Front Office (FO). Tests ensure that uninstalled modules correctly remove their associated UI elements, such as "Add to Wishlist" buttons or API scope checkboxes. Sources: tests/UI/campaigns/modules/02_blockwishlist/01_installation/05_uninstallAndDeleteModule.ts:98-124, tests/UI/campaigns/modules/07_ps_apiresources/01_installation/01_uninstallAndInstallModule.ts:107-129
Technical Utilities
The Tools class provides helper methods used by the module system, such as ZipExtract for module installation and deleteDirectory for clean uninstallation. Sources: classes/Tools.php:1316-1335, 1071-1100
Summary
The Modules & Hook System provides the primary extension mechanism for PrestaShop. Modules manage their own lifecycle through standardized commands, while hooks offer a flexible way for these modules to interact with both the legacy core and modern Symfony-based components like Grids and API resources. Sources: README.md, src/PrestaShopBundle/Command/GenerateHooksDocumentationCommand.php