|
| 1 | +<?php namespace ProcessWire; |
| 2 | + |
| 3 | +/** |
| 4 | + * ProcessWire 'Hello world' demonstration module |
| 5 | + * |
| 6 | + * Demonstrates the Module interface and how to add hooks. |
| 7 | + * |
| 8 | + * See README file for further links regarding module development. |
| 9 | + * |
| 10 | + * This file is licensed under the MIT license |
| 11 | + * https://processwire.com/about/license/mit/ |
| 12 | + * |
| 13 | + * ProcessWire 3.x, Copyright 2016 by Ryan Cramer |
| 14 | + * https://processwire.com |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +class Helloworld extends WireData implements Module { |
| 19 | + |
| 20 | + /** |
| 21 | + * getModuleInfo is a module required by all modules to tell ProcessWire about them |
| 22 | + * |
| 23 | + * @return array |
| 24 | + * |
| 25 | + */ |
| 26 | + public static function getModuleInfo() { |
| 27 | + |
| 28 | + return array( |
| 29 | + |
| 30 | + // The module'ss title, typically a little more descriptive than the class name |
| 31 | + 'title' => 'Hello World', |
| 32 | + |
| 33 | + // version number |
| 34 | + 'version' => 3, |
| 35 | + |
| 36 | + // summary is brief description of what this module is |
| 37 | + 'summary' => 'An example module used for demonstration purposes.', |
| 38 | + |
| 39 | + // Optional URL to more information about the module |
| 40 | + 'href' => 'https://processwire.com', |
| 41 | + |
| 42 | + // singular=true: indicates that only one instance of the module is allowed. |
| 43 | + // This is usually what you want for modules that attach hooks. |
| 44 | + 'singular' => true, |
| 45 | + |
| 46 | + // autoload=true: indicates the module should be started with ProcessWire. |
| 47 | + // This is necessary for any modules that attach runtime hooks, otherwise those |
| 48 | + // hooks won't get attached unless some other code calls the module on it's own. |
| 49 | + // Note that autoload modules are almost always also 'singular' (seen above). |
| 50 | + 'autoload' => true, |
| 51 | + |
| 52 | + // Optional font-awesome icon name, minus the 'fa-' part |
| 53 | + 'icon' => 'smile-o', |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Initialize the module |
| 59 | + * |
| 60 | + * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called |
| 61 | + * when ProcessWire's API is ready. As a result, this is a good place to attach hooks. |
| 62 | + * |
| 63 | + */ |
| 64 | + public function init() { |
| 65 | + |
| 66 | + // add a hook after the $pages->save, to issue a notice every time a page is saved |
| 67 | + $this->pages->addHookAfter('save', $this, 'example1'); |
| 68 | + |
| 69 | + // add a hook after each page is rendered and modify the output |
| 70 | + $this->addHookAfter('Page::render', $this, 'example2'); |
| 71 | + |
| 72 | + // add a 'hello' method to every page that returns "Hello World" |
| 73 | + // use "echo $page->hello();" in your template file to display output |
| 74 | + $this->addHook('Page::hello', $this, 'example3'); |
| 75 | + |
| 76 | + // add a 'hello_world' property to every page that returns "Hello [user]" |
| 77 | + // use "echo $page->hello_world;" in your template file to display output |
| 78 | + $this->addHookProperty('Page::hello_world', $this, 'example4'); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Example1 hooks into the pages->save method and displays a notice every time a page is saved |
| 83 | + * |
| 84 | + * @param HookEvent $event |
| 85 | + * |
| 86 | + */ |
| 87 | + public function example1($event) { |
| 88 | + /** @var Page $page */ |
| 89 | + $page = $event->arguments[0]; |
| 90 | + $this->message("Hello World! You saved {$page->path}."); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + /** |
| 95 | + * Example2 hooks into every page after it's rendered and adds "Hello World" text at the bottom |
| 96 | + * |
| 97 | + * @param HookEvent $event |
| 98 | + * |
| 99 | + */ |
| 100 | + public function example2($event) { |
| 101 | + |
| 102 | + /** @var Page $page */ |
| 103 | + $page = $event->object; |
| 104 | + |
| 105 | + // don't add this to the admin pages |
| 106 | + if($page->template == 'admin') return; |
| 107 | + |
| 108 | + // add a "Hello World" paragraph right before the closing body tag |
| 109 | + $event->return = str_replace("</body>", "<p>Hello World!</p></body>", $event->return); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Example3 adds a 'hello' method (not property) to every page that simply returns "Hello World" |
| 114 | + * |
| 115 | + * @param HookEvent $event |
| 116 | + * |
| 117 | + */ |
| 118 | + public function example3($event) { |
| 119 | + $event->return = "Hello World"; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Example 4 adds a 'hello_world' property (not method) to every page that returns "Hello [user]" |
| 124 | + * |
| 125 | + * @param HookEvent $event |
| 126 | + * |
| 127 | + */ |
| 128 | + public function example4($event) { |
| 129 | + $event->return = "Hello " . $this->user->name; |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments