diff options
Diffstat (limited to 'documentation/examples/hooks')
-rw-r--r-- | documentation/examples/hooks/advanced.php | 28 | ||||
-rw-r--r-- | documentation/examples/hooks/all.php | 12 | ||||
-rw-r--r-- | documentation/examples/hooks/basic.php | 17 | ||||
-rw-r--r-- | documentation/examples/hooks/trigger.php | 14 |
4 files changed, 71 insertions, 0 deletions
diff --git a/documentation/examples/hooks/advanced.php b/documentation/examples/hooks/advanced.php new file mode 100644 index 000000000..ca036c46a --- /dev/null +++ b/documentation/examples/hooks/advanced.php @@ -0,0 +1,28 @@ +<?php +/** + * This snippet demonstrates how to change the value of a hook. The content + * passed into the hook is 'This is some Sample Content.'. After the two hook + * handlers are done, the new content is 'This is some $@mple Content.'. + */ + +// the output:page hook is triggered by elgg_view_page(). +elgg_register_plugin_hook_handler('output', 'page', 'example_plugin_hook_handler', 600); +elgg_register_plugin_hook_handler('output', 'page', 'example_plugin_hook_handler_2', 601); + +function example_plugin_hook_handler($hook, $type, $value, $params) { + // change a to @ + $value = str_replace('a', '@', $value); + + return $value; +} + +function example_plugin_hook_handler_2($hook, $type, $value, $params) { + // change S to $ + $value = str_replace('S', '$', $value); + + return $value; +} + +$content = 'This is some Sample Content.'; + +echo elgg_view_page('Title', $content); diff --git a/documentation/examples/hooks/all.php b/documentation/examples/hooks/all.php new file mode 100644 index 000000000..76b562335 --- /dev/null +++ b/documentation/examples/hooks/all.php @@ -0,0 +1,12 @@ +<?php +/** + * This snippet demonstrates how to register for multiple hooks with the same + * type. + */ + +elgg_register_plugin_hook_handler('all', 'system', 'example_plugin_hook_handler'); + +// This function will be called for any hook of type 'system' +function example_plugin_hook_handler($hook, $type, $value, $params) { + // logic here. +} diff --git a/documentation/examples/hooks/basic.php b/documentation/examples/hooks/basic.php new file mode 100644 index 000000000..734d9e884 --- /dev/null +++ b/documentation/examples/hooks/basic.php @@ -0,0 +1,17 @@ +<?php +/** + * The handler for a plugin hook receives information about the hook (name and + * type), the current value for the hook, and parameters related to the hook. + */ + +elgg_register_plugin_hook_handler('forward', '404', 'example_plugin_hook_handler'); + +function example_plugin_hook_handler($hook, $type, $value, $params) { + var_dump($hook); + var_dump($type); + var_dump($value); + var_dump($params); + + // we are not changing $value so return null + return null; +} diff --git a/documentation/examples/hooks/trigger.php b/documentation/examples/hooks/trigger.php new file mode 100644 index 000000000..4216fd6c0 --- /dev/null +++ b/documentation/examples/hooks/trigger.php @@ -0,0 +1,14 @@ +<?php +/** + * The current value for the hook is passed into the trigger function. Handlers + * can change this value. In this snippet, we check if the value of true was + * changed by the handler functions. + */ + +$result = elgg_trigger_plugin_hook('get_status', 'example', null, true); + +if ($result) { + var_dump('Plugin hook says ok!'); +} else { + var_dump('Plugin hook says no.'); +} |