Anatomy of a Module

Modules are the most powerful and general-purpose of the four types of extensions. Modules are structurally very simple – they simply extend the Modules class with a class named after the file and its containing directory (using camelization rules), providing methods that correspond to trigger calls and filters.

class CamelizedFolderName extends Modules {
    # Camelization accepts hyphens and underscores: foo-bar and foo_bar

    public function this_is_a_callback($arg) {
        # Do your thing
    }

    public function this_is_a_filter($target, $arg) {
        # Do your thing to $target
        return $target;
        # You may also take $target by reference and not have to return it.
    }
}

Module Functions

All of these functions are optional.

public function __init()

This function is called after all of the modules and feathers are instantiated. It exists because calling other triggers in your module's __construct() function would be problematic because not every extension is ready to react.

public static function __install()

This function is called when the module is enabled. Use this for setting up configuration settings, creating database tables, adding group permissions, etc.

public static function __uninstall($confirm)

This function is called when the module is disabled. There is one possible argument, and that's if your module has a confirm metadata item; the argument will be a boolean of whether or not the user confirmed the dialogue.

In typical usage, __uninstall() drops any database tables (if $confirm is true – but that's up to you), removes configuration settings, and removes group permissions that the module added.

Module Construct Functions

The Modules class provides a few functions intended to be used in __construct() or __init().

$this->setPriority()

This function is used for registering a trigger responder in your module with a numeric priority. Prioritized functions will be executed, in ascending order of priority, before all other functions for a given trigger.

A typical use of this function is ensuring your module's trigger responder is called before another:

function __init() {
    $this->setPriority("markup_post_text", 8);
}
$this->addAlias()

This function is used for registering a trigger responder using a different function name in your module. The optional third parameter is the numeric priority for your trigger responder; the default is 10.

function __init() {
    $this->addAlias("markup_post_text", "foo_bar", 10);
}