Skip to content

Anatomy of a Module

Daniel Pimley edited this page Jan 5, 2016 · 31 revisions

Modules are the most powerful and general-purpose of the four types of extensions. Modules are structurally very simple – they’re just a class named after the directory that contains the module components, camelized (a directory named foo_bar or foo-bar would yield the class name FooBar), with functions corresponding 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, whatever you want.

public static function __uninstall($confirm = false)

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

There are a few functions that the Modules class provides to modules, mainly to be used in the __construct() function.

$this->addAlias()

This function is used for aliasing a Trigger to a different function name in your module:

function __construct() {
    $this→addAlias(“markup_post_text”, “foo_bar”);
}
$this->setPriority()

This function is used for setting a priority for a Trigger in your module. The default priority is 10; this default is set even if this function is not called.

A typical use of this function is ensuring your module’s function is called before another. For example, a Markup Module that supports external references (typically at the bottom of your post) might be truncated by a Read More Module. To prevent this from happening, simply set the priority of the filter function for the Markup Module (typically “markup_post_text” or some variation thereof) to a lower number value, such as 8, in order to assign a higher priority:

function __construct() {
    $this→setPriority(“markup_post_text”, 8);
}
Clone this wiki locally