aboutsummaryrefslogtreecommitdiff
path: root/documentation/examples/events
diff options
context:
space:
mode:
Diffstat (limited to 'documentation/examples/events')
-rw-r--r--documentation/examples/events/advanced.php13
-rw-r--r--documentation/examples/events/all.php30
-rw-r--r--documentation/examples/events/basic.php17
-rw-r--r--documentation/examples/events/trigger.php11
4 files changed, 71 insertions, 0 deletions
diff --git a/documentation/examples/events/advanced.php b/documentation/examples/events/advanced.php
new file mode 100644
index 000000000..73edea9da
--- /dev/null
+++ b/documentation/examples/events/advanced.php
@@ -0,0 +1,13 @@
+<?php
+/**
+ * This snippets demonstrates how returning false changes the normal operation
+ * of Elgg.
+ */
+
+elgg_register_event_handler('create', 'object', 'example_event_handler');
+
+function example_event_handler($event, $type, $object) {
+ // Don't allow any non-admin users to create objects
+ // Returning false from this function will halt the creation of the object.
+ return elgg_is_admin_logged_in();
+}
diff --git a/documentation/examples/events/all.php b/documentation/examples/events/all.php
new file mode 100644
index 000000000..0ad02c1d4
--- /dev/null
+++ b/documentation/examples/events/all.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * If you register an 'all' string for the event name, the handler function will
+ * be called for all events with that name, regardless of event type. The same
+ * can be done for the event type argument. Registering 'all' for both
+ * argyuments results in a handler being called for every event.
+ */
+
+elgg_register_event_handler('all', 'object', 'example_event_handler');
+
+// This function will be called for any event of type 'object'
+function example_event_handler($event, $type, $object) {
+ // check what sort of object is passed
+ if ($object instanceof ElggObject) {
+ $subtype = $object->getSubtype();
+
+ switch ($subtype) {
+ case 'blog':
+ case 'thewire':
+ case 'pages':
+ // prevent these object subtypes from being saved or changed
+ return false;
+ default:
+ return true;
+ }
+
+ }
+
+ return true;
+}
diff --git a/documentation/examples/events/basic.php b/documentation/examples/events/basic.php
new file mode 100644
index 000000000..ca2762344
--- /dev/null
+++ b/documentation/examples/events/basic.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * This snippet demonstrates how to register for an event. It dumps the
+ * parameters that the handler receives to the screen. The third argument
+ * of the handler function is an object that is related to the event. For
+ * the 'init', 'system' eveny, it is null.
+ */
+
+elgg_register_event_handler('init', 'system', 'example_event_handler');
+
+function example_event_handler($event, $type, $object) {
+ var_dump($event);
+ var_dump($type);
+ var_dump($object);
+
+ return true;
+}
diff --git a/documentation/examples/events/trigger.php b/documentation/examples/events/trigger.php
new file mode 100644
index 000000000..6ce3a76f0
--- /dev/null
+++ b/documentation/examples/events/trigger.php
@@ -0,0 +1,11 @@
+<?php
+/**
+ * These two snippets demonstrates triggering an event and how to register for
+ * that event.
+ */
+
+$object = new ElggObject();
+elgg_trigger_event('test', 'example', $object);
+
+// elsewhere a handler could be registered by saying
+elgg_register_event_handler('test', 'example', 'example_event_handler');