events()
method of your plugin class, and return an associative array with event names as the key, and the event type as the value.
Example/Example.php
<?php
class ExamplePlugin extends MantisPlugin {
...
function events() {
return array(
'EVENT_EXAMPLE_FOO' => EVENT_TYPE_EXECUTE,
'EVENT_EXAMPLE_BAR' => EVENT_TYPE_CHAIN,
);
}
}
Note
foo()
and a bar()
methods in our plugin class, then hook them to the events we declared earlier. We'll also hook the standard EVENT_MENU_MAIN
event, to link the custom page we created in Section 4.2.3, “Pages and Files”.
Example/Example.php
<?php
class ExamplePlugin extends MantisPlugin {
...
function hooks() {
return array(
'EVENT_MENU_MAIN' => 'menu',
'EVENT_EXAMPLE_FOO' => 'foo',
'EVENT_EXAMPLE_BAR' => 'bar',
);
}
}
$p_event
parameter, which contains the event name triggering the method (allowing a single method to respond to multiple events). The bar()
method also accepts and returns a second parameter, in order to match the expectations of chained events.
Example/Example.php
<?php
class ExamplePlugin extends MantisPlugin {
...
function menu() {
$t_menu[] = array(
'title' => $this->name,
'url' => plugin_page( 'foo' ),
'access_level' => ANYBODY,
'icon' => 'fa-smile-o'
);
return $t_menu;
}
function foo( $p_event ) {
echo 'In method foo(). ';
}
function bar( $p_event, $p_chained_param ) {
return str_replace( 'foo', 'bar', $p_chained_param );
}
}
Example/pages/foo.php
...
<p>
Custom event hooks:
<?php
event_signal( 'EVENT_EXAMPLE_FOO' );
$t_string = 'A sentence with the word "foo" in it.';
$t_new_string = event_signal( 'EVENT_EXAMPLE_BAR', array( $t_string ) );
echo $t_new_string;
?>
</p>
foo()
method will execute and echo a string. After that, the second event "bar" is signaled, and the page passes a string parameter; the plugin's bar()
gets the string and replaces any instance of "foo" with "bar", and returns the resulting string. If any other plugin had hooked the event, that plugin could have further modified the new string from the Example plugin, or vice versa, depending on the loading order of plugins. The page then echos the modified string that was returned from the event.