Permalink
123 lines (97 sloc) 3.59 KB
<?php
/**
* ProcessWire 'Hello world' demonstration module
*
* Demonstrates the Module interface and how to add hooks.
*
* See README file for further links regarding module development.
*
* ProcessWire 2.x
* Copyright (C) 2015 by Ryan Cramer
* This file licensed under Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
*
* https://processwire.com
*
*/
class Helloworld extends WireData implements Module {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
// The module'ss title, typically a little more descriptive than the class name
'title' => 'Hello World',
// version number
'version' => 2,
// summary is brief description of what this module is
'summary' => 'An example module used for demonstration purposes. See the /site/modules/Helloworld.module file for details.',
// Optional URL to more information about the module
'href' => 'https://processwire.com',
// singular=true: indicates that only one instance of the module is allowed.
// This is usually what you want for modules that attach hooks.
'singular' => true,
// autoload=true: indicates the module should be started with ProcessWire.
// This is necessary for any modules that attach runtime hooks, otherwise those
// hooks won't get attached unless some other code calls the module on it's own.
// Note that autoload modules are almost always also 'singular' (seen above).
'autoload' => true,
// Optional font-awesome icon name, minus the 'fa-' part
'icon' => 'smile-o',
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init() {
// add a hook after the $pages->save, to issue a notice every time a page is saved
$this->pages->addHookAfter('save', $this, 'example1');
// add a hook after each page is rendered and modify the output
$this->addHookAfter('Page::render', $this, 'example2');
// add a 'hello' method to every page that returns "Hello World"
// use "echo $page->hello();" in your template file to display output
$this->addHook('Page::hello', $this, 'example3');
// add a 'hello_world' property to every page that returns "Hello [user]"
// use "echo $page->hello_world;" in your template file to display output
$this->addHookProperty('Page::hello_world', $this, 'example4');
}
/**
* Example1 hooks into the pages->save method and displays a notice every time a page is saved
*
*/
public function example1($event) {
$page = $event->arguments[0];
$this->message("Hello World! You saved {$page->path}.");
}
/**
* Example2 hooks into every page after it's rendered and adds "Hello World" text at the bottom
*
*/
public function example2($event) {
$page = $event->object;
// don't add this to the admin pages
if($page->template == 'admin') return;
// add a "Hello World" paragraph right before the closing body tag
$event->return = str_replace("</body>", "<p>Hello World!</p></body>", $event->return);
}
/**
* Example3 adds a 'hello' method (not property) to every page that simply returns "Hello World"
*
*/
public function example3($event) {
$event->return = "Hello World";
}
/**
* Example 4 adds a 'hello_world' property (not method) to every page that returns "Hello [user]"
*
*/
public function example4($event) {
$event->return = "Hello " . $this->user->name;
}
}