ProcessWire - Module schreiben

Aus Wikizone
Version vom 16. Dezember 2019, 19:43 Uhr von 84.136.104.26 (Diskussion) (Die Seite wurde neu angelegt: „== Beispiele == == ProcessWire Hello World Modul == Beispiel Modul mit Hooks (liegt immer in der Standardinstallation) <syntaxhighlight lang="php"> <?php names…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

Beispiele

ProcessWire Hello World Modul

Beispiel Modul mit Hooks (liegt immer in der Standardinstallation)

<?php namespace ProcessWire;

/**
 * ProcessWire 'Hello world' demonstration module
 *
 * Demonstrates the Module interface and how to add hooks.
 * 
 * See README file for further links regarding module development.
 * 
 * This file is licensed under the MIT license
 * https://processwire.com/about/license/mit/
 * 
 * ProcessWire 3.x, Copyright 2016 by Ryan Cramer
 * 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's title, typically a little more descriptive than the class name
			'title' => 'Hello World', 

			// version number 
			'version' => 3, 

			// summary is brief description of what this module is
			'summary' => 'An example module used for demonstration purposes.',
			
			// 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
	 * 
	 * @param HookEvent $event
	 *
	 */
	public function example1($event) {
		/** @var Page $page */
		$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
	 * 
	 * @param HookEvent $event
	 *
	 */
	public function example2($event) {

		/** @var Page $page */
		$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"
	 * 
	 * @param HookEvent $event
	 *
	 */
	public function example3($event) {
		$event->return = "Hello World";
	}

	/**
	 * Example 4 adds a 'hello_world' property (not method) to every page that returns "Hello [user]"
	 * 
	 * @param HookEvent $event
	 *
	 */
	public function example4($event) {
		$event->return = "Hello " . $this->user->name; 
	}
	
}