-
Notifications
You must be signed in to change notification settings - Fork 3
Modules
Modules are made up of methods that run at certain times during the program's execution. These "certain times" are called Hooks. There are two types of hooks: Actions and Filters.
Actions are defined points in the code that a module can use to hook in their method. A good example would be an Action called "sleepy_preprocess". This action runs very early in the program's lifecycle allowing you to run code before any templates have been rendered. It can be used to verify that a user has access to a page, or to start pulling content from a web service.
namespace Module\ExampleModule;
use \Sleepy\Core\Hook;
use \Sleepy\Core\Module;
class Example1 extends Module
{
public $hooks = [
'sleepy_preprocess' => 'start'
];
public function start()
{
// Check if a user is logged in
}
}
Hook::register(new Example1());Filters are similar to Actions, but they are used to modify data. An example of a filter could be "render_placeholder_title". This is the Filter for the placeholder "title". In this example, we might want to add the string " | Website Name" to the end of the titles for SEO purposes.
namespace Module\ExampleModule; # It's good practice to namespace your module
use \Sleepy\Core\Hook; # We need both of these classes
use \Sleepy\Core\Module; # to create a module
class Example2 extends Module
{
public $hooks = [
'render_placeholder_title' => 'addSite' # We are going to use this filter -> method
];
public function addSite($str) # The filter will get the value of the title placeholder as $str
{
return $str + ' | Website Name'; # and we return the modified string to the template for use
}
}
Hook::register(new Example2());