Hooks are places inside a code file that allow functions in other code files to be run by having them hook into the current code.
When to Expose a Hook
It is best to expose a hook when you want a section in your custom code to be available to be hooked into by other code files. This exposed section will allow code to be run within your custom code file.
An exposed hook can be called from within the same app or from other custom apps. This makes hooks very versatile in how they are accessed within the Kayako suite.
When to Use a Hook
To determine if a hook should be used depends on the goal that needs to be achieved. If the functionality that is required to be added needs to manipulate or show data in a app that is not updated by you then utilizing a hook is recommended. This method preferred to manually modifying this code because when an update to the app is released the custom changed to it will not have to be re-added manually.
When a situation as described above is encountered then that is when using an available hook is recommended.
How to Use a Hook
Once it has been determined that a hook is what is needed or needs to be utilized the following steps outline how to go about doing so.
Creating a Hook
To create a hook in code that will allow others to run custom code within the created file one must add a simple line of code. This line of code will allow another developer to create a hook file that will be executed automatically.
The line of code is laid out as follows:
($_hookCode = SWIFT_Hook::Execute('HOOK_NAME')) ? eval($_hookCode) : false;
The only stipulation about hooks is that the name must be unique for the app that it is for. Once the hook code has been added to the code file the hook is ready to be used.
Using a Hook
A hook is used by creating a hook file in the app where the desired hook to use is located. This file will contain the code that is to be run.
Creating the file
The hook file is created within the app where the hook to be used is found. All hook files are contained into a directory called hooks.
The hook file it self follows a specific naming convention, this convention is HOOK_NAME.hook where HOOK_NAME is the same as the HOOK_NAME in the creating hook code above.
This is an example of how the hook directory and files are set up.
Coding the file
The code inside the hook file is PHP calls that are not contained within a class or function. The code inside this file should be kept as minimal as possible by keeping all complicated calls within functions that are called from inside the hook file.
The code within this file is executed within the class and function that the hook resides in. As a result the code will have access to all the same functions and variables within that function.
This is an example of how the code within a hook file is laid out.
<?php echo "You are running code inside a hook file!"; ?>
From inside this hook file libraries can be loaded and more complicated functions can be called to manipulate data or display more information to the user of the web portal.
When the app that these created hook files resides in is updated the upgrade will never overwrite or remove the hooks directory. This means all of the customization that has been added is retained between updates and upgrades and does not require additional altering of code each update.
Example of using "Hooks" :
A step by step guide illustrating the functionality of "Hooks" in our help desk is explained here : organization_tickets hook.pdf. Files required to use the sample "Hook" explained in the documentation are available here : organizationtickets.rar