Skip to end of metadata
Go to start of metadata
Table of contents

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:

The only stipulation about hooks is that the name must be unique for the module 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 module 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 module 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.

Example

This is an example of how the hook directory and files are set up.

__modules/modulename/hooks/HOOK_NAME.hook

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.

Example

This is an example of how the code within a hook file is laid out.

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.

Upgrading

When the module 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.

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Mar 30, 2012

    For system hooks hook file MUST be located in your CUSTOM module hooks folder, not in system module, as stated in this thread http://forums.kayako.com/threads/client_init-hook-where-to-put-it.29656/