-
-
Notifications
You must be signed in to change notification settings - Fork 45
Anatomy of a Module
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 andfilters.
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.
}
}
All of these functions are optional.
__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.
__install()
is called when the Module is enabled. Use this for setting up configuration settings, creating database tables, adding group permissions, whatever you want.
__uninstall()
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 (unless $confirm
is true
, but that’s up to you), removes configuration settings, and removes group permissions that the Module added.
There are a few functions that the Modules
class provides to Modules, mainly to be used in the __construct()
function.
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 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);
}
This is the wiki for Chyrp Lite: An ultra-lightweight blogging engine, written in PHP.
- About Permissions
- Tour of a Theme
- Twig Reference
- Twig Variables
- Object Attributes
- Routes and Controllers
- Making Your First Module
- Debug and Tester Modes
- About Errors
- Introduction to Helpers
- Introduction to Translations
- Introduction to Triggers
- Anatomy of info.php Files
- Anatomy of a Feather
- Anatomy of a Module
- Anatomy of a Theme
- Anatomy of a Post
- Localizing Extensions
- Adding Ajax Functionality
- Working with JavaScript
- Working with Model
- Working with Config