Modules
Modules allow to create extra functionality that does not touch the core files of 68KB. Thus enabling your module to not get overwritten during upgrades.
Module Requirements
- Each module must be contained in a single directory.
- Each module must have a config.php file that includes needed information.
- The users should be able to activate and use the module from the “Modules” menu in the administrative. Optimally, a module should not require users to modify the source code.
- If the module is going to interact with 68KB hook system it must contain a file named events.php
- index.php and admin.php and used if the module has a frontend area or an administration section. These are only required if the user needs to have some of interaction with the module.
- You may include any other files that the module may need to interact with.
Naming Your Module
The first step is to name your module. We will be building an example developer module that you can find in the my-modules directory. We will name this module “developer”.
Once we have the name of our module, we need to create a folder for it in the my-modules folder. The name of this folder should be a lowercased and space-less version of the module's name. Instead of spaces, you should either use a single word for your module or underscores. For our example, we will create a folder called “developer” that would be located like so: /my-modules/developer/.
Create a configuration file
We will need to create a new php file named config.php that will reside in the /my-modules/developer/ folder.
The config.php file should look like this:
<?php
$data['module']['name'] = "developer";
$data['module']['displayname'] = "Developer Module";
$data['module']['description'] = "This module is used to show developers how the module system works.";
$data['module']['version'] = "v1.0";
- name:
- - The name is the folder name of your module.
- Display Name:
- - This is the title shown in the administration -> settings -> modules page.
- Description:
- - This is the description shown in the administration -> settings -> modules page.
- Version:
- - This is the version of the module. The version is left up to you.
Create an init.php
The init.php file is a procedural php file responsible for installing items into the database and uninstalling if the user decides to remove the module.
The init.php file can have three functions and they are:
install()
upgrade()
uninstall()
These functions are not required if you do not need to insert or alter the database.
You can see an example of this file in my-modules/developer/init.php
Please note the upgrade is only ran if you upload new module files with a higher version number than the one stored in the database.
Create an events.php
The events.php file is responsible for integrating functionality in the core of the script. Here is an example file:
<?php
class developer_events
{
function __construct(&$core_events)
{
$core_events->register('settings/form', $this, 'test_settings');
$core_events->register('settings/validation', $this, 'test_validation');
}
function test_settings()
{
$output = '
<tr>
<td class="row1"><label for="testing">Testing:</label></td>
<td class="row1"><input type="text" size="50" name="testing" id="testing" value="'. set_value('testing') .'" /></td>
</tr>
';
return $output;
}
function test_validation()
{
$CI =& get_instance();
return $CI->form_validation->set_rules('max_test', 'lang:kb_max_search', 'required|numeric');
}
}
The events.php file needs a class named name_events. The name comes from the name of your module.
This code basically does two things. First in the constructor it registers the event you want and then when that hook is called it runs your method. In the example above we register the settings/form hook to the $this->test_settings method.