Skip to content

Anatomy of a Module

etheon edited this page Sep 12, 2010 · 18 revisions
class CamelizedFolderName extends Modules { # Camelization accepts 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.
	}
}

Modules are very simple – they’re just a class named by the folder, camelized (foo_bar to FooBar), with functions corresponding to Trigger calls and filters.

See: Triggers

Module Functions

All of these functions are optional.

public function __init()

__init() is exactly like the __construct() function, but it’s only called after all of the Modules and Feathers are instantiated. It exists because otherwise, calling other Triggers in __construct() would be problematic because not every Module/Feather is ready to react.

public static function __install()

__install() 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)

__uninstall() is called when the Module is disabled. There is one possible argument, and that’s if your Module has a “confirm” YAML setting; the argument will be a boolean of whether or not they confirmed the dialogue.

In typical usage, __uninstall() drops any database tables (unless $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()”:http://chyrp.net/docs/class/Module-php.html#Module.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()”:http://chyrp.net/docs/class/Module-php.html#Module.setPriority

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

A typical use of this function is making sure 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 cut off/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 something higher, like 8:

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