Skip to content
This repository has been archived by the owner on Nov 24, 2019. It is now read-only.

v1.6.1 Plugin hooks

vee w, edited this page Jun 12, 2015 · 1 revision

There are some hook actions and filters for Fuel Start project. These plugin hooks are for access or modify core code process without write into it directly.

Plugin hook index

Hook type Hook name Arguments Description
filter AccountHashPassword $password Change the way to hash password.
action AccountCheckPassword $entered_password, $hashed_password, $account_object
The account object is object data that got from check username or email first.
Change the way to check password.
action AccountLoginSuccess $account_id, $account_object
The account object is db object.
Action to do after login success such as hook into other complete script like a forum or anything else.
action AccountDeleteAvatar $account_id, $account_object
The account object is db object.
Action to do after deleted avatar.
action AccountAdminEditAccount $account_id, array('input_data' => array data, 'input_data_fields' => array data, 'input_data_level' => array data, 'inputs_post' => \Input::post(), 'email_change' => (boolean), 'password_changed' => (boolean)) Action to do after admin completed edit account.
action AccountMemberEditAccount $account_id, array('input_data' => array data, 'input_data_fields' => array data, 'inputs_post' => \Input::post(), 'email_change' => (boolean), 'password_changed' => (boolean)) Action to do after member completed edit their account.
action AccountDeleteOnMultisiteTables $account_id, $site_id Delete data row in anytable you want on multisite table.
action AccountBeforeDeleteAccount $account_id Action to do before delete account.
action AccountAfterRegister array('input_data' => array data, 'input_data_fields' => array data, 'inputs_post' => \Input::post())
This hook has only one argument that is array
Action to do after user register their account. (The confirm status is up to your configuration in admin page.)
action AccountControllerAfterConfirmedRegister array('input_username' => string data, 'inputs_post' => \Input::post())
This hook has only one argument that is array
Action to do after user confirmed register by email.
action AccountConfirmRegisterProcess $data
Data is array
Override account confirmation after registered
action LevelGroupAfterDeleted $level_group_id Action to do after deleted level group (or role).
action SitesAfterDeleteSite $site_id Action to do after deleted sub sites.
filter SitesGetDefaultConfigValueForAddSite If you add config value to db as core then you needs to hook this filter to reset to default value after added a sub site.
The returned value must be this array format.
$arr['config_name'] = 'default value';
filter SitesGetModulesMultisiteTables This will get module's tables that need to be copy while create new site.
filter BaseControllerGenTitle $title, $name_position, $config
Title is string.
Name position can be first or last.
Config is array with 'site_name' and 'page_title_separator' key. To access, use this code. $config['site_name']['value']
Override the way to generate title on base controller.

Hook type

There are two hook types now.
First is action hook. The action hook may no need the return result. This is up to what is that hook and its requirement.
Second is filter hook. The filter hook always need the return result.

Access these hooks.

Create the module for use as plugin. (You may use your already have modules.) For example: we will create MyAccount module.
Create myaccount folder.
Create myaccount/myaccount_module.php file.

Add module's name class to _module.php file.

<?php
class MyAccount_Module
{

    // your method here.

}

Write the hook action/filter with name as method.
Let says that we have this hook filter name AccountHashPassword and its argument is only one. We will name this as $password.
For more arguments (if there are) will merge into array and put in argument 2. So if there are more arguments support and you want to use it, you must get it from the second argument.

<?php
class MyAccount_Module
{

    /**
     * Change the way of hashing password.
    */
    public function filterAccountHashPassword($password = '', $args = '')
    {
        // your main argument is $password. 
        // $args is for in case that this is other plugin method
        // you can get more data from this $args array.

        // warning! this is just example. do not use this password hashing in your project.
        return md5($password);
    }// filterAccountHashPassword

}

Create your hook

To create your hook is easy.
1 Create new plugin object.

$plugin = new \Library\Plugins();

2 Check that are there any hook filters or actions? The hook name must be StudlyCaps. This is refer to PSR, the hook name will be after hook type so it must be StudlyCaps.

$plugin = new \Library\Plugins();

// for hook filter.
if ($plugin->hasFilter('ModifyMyContent')) {
    // and you create module class with filterModifyMyContent method in it.
}

// for hook action.
if ($plugin->hasAction('RecordAdminAction')) {
    // create your module class with actionRecordAdminAction method in it.
}

3 Start hook and get the result. You may pass the data into arguments as much as you want or just one with array key.

$plugin = new \Library\Plugins();

// for hook filter.
if ($plugin->hasFilter('ModifyMyContent')) {
    $my_content = ['content' => 'This is page content with [b]BB[/b] code.', 'sign' => 'Vee W.'];
    $hook_filter_result = $plugin->doFilter('ModifyMyContent', $my_content);
    if (is_array($hook_filter_result)) {
        // in case that filter result you expect as array.
        $content = (array_key_exists('content', $hook_filter_result) ? $hook_filter_result['content'] : 'No hook result. You may use original content data.');
        $sign = (array_key_exists('sign', $hook_filter_result) ? $hook_filter_result['sign'] : 'No sign. You may use original sign data.');
    }
    // in case that filter result you expect as string or number, you can set it into variable and ready for use.
}

// for hook action.
if ($plugin->hasAction('RecordAdminAction')) {
    $page = \Uri::string();
    $all_inputs = \Input::all();
    $hook_action_result = $plugin->doAction('RecordAdminAction', $page, $all_inputs);
    // in case that you expect action result, please use the code below.
    if (is_array($hook_action_result) && array_key_exists('RecordAdminAction', $hook_action_result)) {
        $hook_action_result = array_shift($hook_action_result['RecordAdminAction']);
        if (is_bool($hook_action_result) && $hook_action_result === true) {
            // assuming that there is at least a plugin and it record admin's action success.
        }
    }
}

More accessible for your hook

In some case you may want to know what hook actions/filters work and how is their results. You can access some plugin properties.

// all of these are array results.
print_r($plugin->track_hook_plugins);
print_r($plugin->track_hook_actions);
print_r($plugin->track_hook_filters);

Or if you want to get filtered data from multiple plugins that were hooked.

$filter_results = $plugin->original_data;
if (is_array($filter_results)) {
    foreach ($filter_results as $each_result) {
        // use $each_result as you wish.
        echo $each_result."<br>\n";
    }
}

Please see the access hook in action in the Fuel Start controllers, models.