Addon hooks are programming code used to perform certain operations with data in the process of executing the events like addon installation, removal, activation, deactivation and rebuild.
Addon Hooks Overview
- init - run on every request to XC
- install - run after the add-on installation
- enable - run after the add-on is enabled (after install, in case of installation)
- disable - run after the add-on is disabled
- remove - run only during deletion of an add-on that cannot be disabled (that has canDisable:false in main.yaml)
- rebuild - run after every rebuild (after install and enable)
- upgrade - run during upgrade (before rebuild)
Addon Hook Format
To store hooks, use separate classes that are DI services.
To subscribe to every hook, add a corresponding tag to the service description:
modules/[Author]/[Name]/Resources/config/services.yaml
XLite\Module\[Author]\[Name]\LifetimeHook\Hook:
tags:
- {name: xcart.lifetime-hook, type: init, method: onInit}
- {name: xcart.lifetime-hook, type: install, method: onInstall}
- {name: xcart.lifetime-hook, type: enable, method: onEnable}
- {name: xcart.lifetime-hook, type: disable, method: onDisable}
- {name: xcart.lifetime-hook, type: rebuild, method: onRebuild}
- {name: xcart.lifetime-hook, type: upgrade, method: onUpgrade, version: '5.5.0.1'}
An add-on can have several hook classes, if it is required for a better code readability. For example, upgrade hooks can be moved to a separate class, or every hook can have a separate class.